run js on server?

  • Thread starter Thread starter David Bartosik - MS MVP
  • Start date Start date
D

David Bartosik - MS MVP

Can I (and how) run a javascript in the code behind on the server and then
send the results of the script to the aspx page. Thereby rendering the
content regardless of the clients javascript support.
 
David,

What is the script supposed to do?

Some of the details of what you are attempting to do server side would help
to solve your problem.

The short answer is no, you can't run a javascript on the server. However,
there is probably a way to do whatever you need.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
On my site I have some content that is delivered via a js file, for example
the page http://www.davidbartosik.com/webdesign.htm runs the following
script :
<script language="JavaScript"
src="http://www.web-source.net/content/website.js"></script>
- the js file contains the html for the browser to render.
This scenario of course is dependant on client support.
I'm wondering if I can run this server side to get the html and then send
the actual html to the client (like to a label control).
 
David,

You could make a request for the page from server side and get the html
response in a string.

It would be something like this:

(Copy and paste the code below to the code behind file of a page named
defaults.aspx)

Imports System.IO

Imports System.Net

Imports System.Text

Public Class _default

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim WebRequest As System.Net.WebRequest =
CType(System.Net.WebRequest.Create(New Uri("http://www.gotdotnet.com")),
System.Net.HttpWebRequest)

' Send the 'WebRequest' and wait for response.

Dim WebResponse As WebResponse = WebRequest.GetResponse()

' Call method 'GetResponseStream' to obtain stream associated with the
response object

Dim ReceiveStream As Stream = WebResponse.GetResponseStream()

Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

' Pipe the stream to a higher level stream reader with the required encoding
format.

Dim readStream As New StreamReader(ReceiveStream, encode)

Response.Write("Response stream received")

Dim read(256) As [Char]

' Read 256 charcters at a time .

Dim count As Integer = readStream.Read(read, 0, 256)

Response.Write("HTML..." + ControlChars.Lf + ControlChars.Cr)

While count > 0

' Dump the 256 characters on a string and display the string onto the
console.

Dim str As New [String](read, 0, count)

Response.Write(str)

count = readStream.Read(read, 0, 256)

End While

' Release the resources of stream object.

readStream.Close()

' Release the resources of response object.

WebResponse.Close()

End Sub

End Class



Sincerely,


--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
David,

This is how you'd do it if the files aren't accessible through the file
system.

You could also use the File system object to access the files directly
without the overhead of making http calls.

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


S. Justin Gengo said:
David,

You could make a request for the page from server side and get the html
response in a string.

It would be something like this:

(Copy and paste the code below to the code behind file of a page named
defaults.aspx)

Imports System.IO

Imports System.Net

Imports System.Text

Public Class _default

Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim WebRequest As System.Net.WebRequest =
CType(System.Net.WebRequest.Create(New Uri("http://www.gotdotnet.com")),
System.Net.HttpWebRequest)

' Send the 'WebRequest' and wait for response.

Dim WebResponse As WebResponse = WebRequest.GetResponse()

' Call method 'GetResponseStream' to obtain stream associated with the
response object

Dim ReceiveStream As Stream = WebResponse.GetResponseStream()

Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")

' Pipe the stream to a higher level stream reader with the required encoding
format.

Dim readStream As New StreamReader(ReceiveStream, encode)

Response.Write("Response stream received")

Dim read(256) As [Char]

' Read 256 charcters at a time .

Dim count As Integer = readStream.Read(read, 0, 256)

Response.Write("HTML..." + ControlChars.Lf + ControlChars.Cr)

While count > 0

' Dump the 256 characters on a string and display the string onto the
console.

Dim str As New [String](read, 0, count)

Response.Write(str)

count = readStream.Read(read, 0, 256)

End While

' Release the resources of stream object.

readStream.Close()

' Release the resources of response object.

WebResponse.Close()

End Sub

End Class



Sincerely,


--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


David Bartosik - MS MVP said:
On my site I have some content that is delivered via a js file, for example
the page http://www.davidbartosik.com/webdesign.htm runs the following
script :
<script language="JavaScript"
src="http://www.web-source.net/content/website.js"></script>
- the js file contains the html for the browser to render.
This scenario of course is dependant on client support.
I'm wondering if I can run this server side to get the html and then send
the actual html to the client (like to a label control).

--
David Bartosik - Microsoft MVP
Visit www.davidbartosik.com
for Publisher and Web Design
Tips and How-to's.

and
then
 
Back
Top