Professional Web Applications Themes

run js on server? - ASP.NET General

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 Bartosik - Microsoft MVP Visit www.davidbartosik.com for Publisher and Web Design Tips and How-to's....

  1. #1

    Default run js on server?

    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 Bartosik - Microsoft MVP
    Visit www.davidbartosik.com
    for Publisher and Web Design
    Tips and How-to's.


    David Guest

  2. #2

    Default Re: run js on server?

    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


    "David Bartosik - MS MVP" <org> wrote in message
    news:phx.gbl... 


    S. Guest

  3. #3

    Default Re: run js on server?


    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.

    "S. Justin Gengo" <com> wrote in message
    news:phx.gbl... 
    help [/ref]
    then 
    >
    >[/ref]


    David Guest

  4. #4

    Default Re: run js on server?

    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" <org> wrote in message
    news:phx.gbl... 
    example 
    > help [/ref]
    However, [/ref]
    > then 
    > >
    > >[/ref]
    >
    >[/ref]


    S. Guest

  5. #5

    Default Re: run js on server?

    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" <com> wrote in message
    news:phx.gbl... 
    InitializeComponent() 
    encoding 
    > example [/ref]
    send 
    > > help [/ref]
    > However, [/ref][/ref]
    and [/ref][/ref]
    the 
    > >
    > >[/ref]
    >
    >[/ref]


    S. Guest

Similar Threads

  1. Replies: 0
    Last Post: August 15th, 09:24 AM
  2. Replies: 2
    Last Post: September 12th, 10:44 PM
  3. Replies: 0
    Last Post: August 23rd, 05:27 PM
  4. Replies: 4
    Last Post: June 11th, 12:16 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139