Dynamically rendering the content of script

Discussion in 'ASP .Net' started by Yama, Oct 21, 2005.

  1. Yama

    Yama Guest

    Hi,

    How can I dynamically embed the content of a filename.js into a .aspx page
    before rendering it to the client?

    Thanks,

    ~yamazed
     
    Yama, Oct 21, 2005
    #1
    1. Advertising

  2. Yama,

    Insert the javascript using Page.RegisterStartupScript. Here's an example:

    If Not page.IsStartupScriptRegistered("TextBoxFocus") Then
    Dim StringBuilder As New System.Text.StringBuilder

    With StringBuilder
    .Append("<script language=""javascript"">" & vbCrLf)
    .Append(vbTab & "<!--" & vbCrLf)
    .Append(vbTab & vbTab & "document.getElementById('" &
    webControl.ClientID & "').focus();" & vbCrLf)
    .Append(vbTab & "//-->" & vbCrLf)
    .Append("</script>" & vbCrLf)
    End With

    page.RegisterStartupScript("TextBoxFocus", StringBuilder.ToString)
    End If

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "Yama" <> wrote in message
    news:...
    > Hi,
    >
    > How can I dynamically embed the content of a filename.js into a .aspx
    > page before rendering it to the client?
    >
    > Thanks,
    >
    > ~yamazed
    >
     
    S. Justin Gengo, Oct 21, 2005
    #2
    1. Advertising

  3. Yama

    Yama Guest

    Hi,

    My javascript has over 3000 lines of code and this will not be practical.
    Anything other way?

    Thanks,

    Yama




    "S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
    message news:...
    > Yama,
    >
    > Insert the javascript using Page.RegisterStartupScript. Here's an example:
    >
    > If Not page.IsStartupScriptRegistered("TextBoxFocus") Then
    > Dim StringBuilder As New System.Text.StringBuilder
    >
    > With StringBuilder
    > .Append("<script language=""javascript"">" & vbCrLf)
    > .Append(vbTab & "<!--" & vbCrLf)
    > .Append(vbTab & vbTab & "document.getElementById('" &
    > webControl.ClientID & "').focus();" & vbCrLf)
    > .Append(vbTab & "//-->" & vbCrLf)
    > .Append("</script>" & vbCrLf)
    > End With
    >
    > page.RegisterStartupScript("TextBoxFocus", StringBuilder.ToString)
    > End If
    >
    > --
    > Sincerely,
    >
    > S. Justin Gengo, MCP
    > Web Developer / Programmer
    >
    > www.aboutfortunate.com
    >
    > "Out of chaos comes order."
    > Nietzsche
    > "Yama" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> How can I dynamically embed the content of a filename.js into a .aspx
    >> page before rendering it to the client?
    >>
    >> Thanks,
    >>
    >> ~yamazed
    >>

    >
    >
     
    Yama, Oct 21, 2005
    #3
  4. Yama

    Bruce Barker Guest

    read the file into string, then write it out as explained. you'd be better
    off using having the browser fetch it because then it would be cached. 3000
    lines is a large payload.

    -- bruce (sqlwork.com)




    "Yama" <> wrote in message
    news:%...
    > Hi,
    >
    > My javascript has over 3000 lines of code and this will not be practical.
    > Anything other way?
    >
    > Thanks,
    >
    > Yama
    >
    >
    >
    >
    > "S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
    > message news:...
    >> Yama,
    >>
    >> Insert the javascript using Page.RegisterStartupScript. Here's an
    >> example:
    >>
    >> If Not page.IsStartupScriptRegistered("TextBoxFocus") Then
    >> Dim StringBuilder As New System.Text.StringBuilder
    >>
    >> With StringBuilder
    >> .Append("<script language=""javascript"">" & vbCrLf)
    >> .Append(vbTab & "<!--" & vbCrLf)
    >> .Append(vbTab & vbTab & "document.getElementById('" &
    >> webControl.ClientID & "').focus();" & vbCrLf)
    >> .Append(vbTab & "//-->" & vbCrLf)
    >> .Append("</script>" & vbCrLf)
    >> End With
    >>
    >> page.RegisterStartupScript("TextBoxFocus", StringBuilder.ToString)
    >> End If
    >>
    >> --
    >> Sincerely,
    >>
    >> S. Justin Gengo, MCP
    >> Web Developer / Programmer
    >>
    >> www.aboutfortunate.com
    >>
    >> "Out of chaos comes order."
    >> Nietzsche
    >> "Yama" <> wrote in message
    >> news:...
    >>> Hi,
    >>>
    >>> How can I dynamically embed the content of a filename.js into a .aspx
    >>> page before rendering it to the client?
    >>>
    >>> Thanks,
    >>>
    >>> ~yamazed
    >>>

    >>
    >>

    >
    >
     
    Bruce Barker, Oct 21, 2005
    #4
  5. Yama

    Ken Varn Guest

    See my codeproject article at

    http://www.codeproject.com/aspnet/DynamicContentRendering.asp

    There is a class in the project called EmbeddedJScript. It basically wraps
    a bunch of .NET functions that allow you to embed a JavaScript file as a
    resource and then dynamically render it to the page.

    --
    -----------------------------------
    Ken Varn
    Senior Software Engineer
    Diebold Inc.

    EmailID = varnk
    Domain = Diebold.com
    -----------------------------------
    "Yama" <> wrote in message
    news:...
    > Hi,
    >
    > How can I dynamically embed the content of a filename.js into a .aspx

    page
    > before rendering it to the client?
    >
    > Thanks,
    >
    > ~yamazed
    >
    >
     
    Ken Varn, Oct 21, 2005
    #5
  6. Yama

    Yama Guest

    Hi Bruce,

    I came up with a better solution:

    First I detect the browser and what type of scripting I'll use. I compile
    each IE6javascript.js (and all others) into the project as an embedded
    resource (select the script's property and change the "Build Action" from
    "Content to Embedded Resource) then in my code:

    Partial Code:
    System.Reflection.Assembly _Assembly = this.GetType().Assembly;

    using( System.IO.Stream stream = _Assembly.GetManifestResourceStream(
    "myAssemby.MyNamespace.MyScriptFolder.IE6javascript.js" ) )

    {

    using( System.IO.StreamReader reader = new System.IO.StreamReader(stream) )

    {

    string _script = reader.ReadToEnd();

    Page.RegisterClientScriptBlock( "KeyReferrerNotUSED", _script );

    }

    Finally perform a cache on server and client side. If the server code
    changes then force the change in the client machine.Hint: I use the Observer
    design pattern along with the factory method design pattern to achieve all
    of this.

    Cheers,



    ~yamazed







    "Bruce Barker" <> wrote in message
    news:...
    > read the file into string, then write it out as explained. you'd be better
    > off using having the browser fetch it because then it would be cached.
    > 3000 lines is a large payload.
    >
    > -- bruce (sqlwork.com)
    >
    >
    >
    >
    > "Yama" <> wrote in message
    > news:%...
    >> Hi,
    >>
    >> My javascript has over 3000 lines of code and this will not be practical.
    >> Anything other way?
    >>
    >> Thanks,
    >>
    >> Yama
    >>
    >>
    >>
    >>
    >> "S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
    >> message news:...
    >>> Yama,
    >>>
    >>> Insert the javascript using Page.RegisterStartupScript. Here's an
    >>> example:
    >>>
    >>> If Not page.IsStartupScriptRegistered("TextBoxFocus") Then
    >>> Dim StringBuilder As New System.Text.StringBuilder
    >>>
    >>> With StringBuilder
    >>> .Append("<script language=""javascript"">" & vbCrLf)
    >>> .Append(vbTab & "<!--" & vbCrLf)
    >>> .Append(vbTab & vbTab & "document.getElementById('" &
    >>> webControl.ClientID & "').focus();" & vbCrLf)
    >>> .Append(vbTab & "//-->" & vbCrLf)
    >>> .Append("</script>" & vbCrLf)
    >>> End With
    >>>
    >>> page.RegisterStartupScript("TextBoxFocus", StringBuilder.ToString)
    >>> End If
    >>>
    >>> --
    >>> Sincerely,
    >>>
    >>> S. Justin Gengo, MCP
    >>> Web Developer / Programmer
    >>>
    >>> www.aboutfortunate.com
    >>>
    >>> "Out of chaos comes order."
    >>> Nietzsche
    >>> "Yama" <> wrote in message
    >>> news:...
    >>>> Hi,
    >>>>
    >>>> How can I dynamically embed the content of a filename.js into a .aspx
    >>>> page before rendering it to the client?
    >>>>
    >>>> Thanks,
    >>>>
    >>>> ~yamazed
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Yama, Oct 22, 2005
    #6
  7. Yama

    Yama Guest

    Hey Ken,

    Not stupid hey! Using the httpModules sounds like an excellent way to handle
    caching of the script.

    Thanks,

    ~yamazed




    "Ken Varn" <nospam> wrote in message
    news:%23n%...
    > See my codeproject article at
    >
    > http://www.codeproject.com/aspnet/DynamicContentRendering.asp
    >
    > There is a class in the project called EmbeddedJScript. It basically
    > wraps
    > a bunch of .NET functions that allow you to embed a JavaScript file as a
    > resource and then dynamically render it to the page.
    >
    > --
    > -----------------------------------
    > Ken Varn
    > Senior Software Engineer
    > Diebold Inc.
    >
    > EmailID = varnk
    > Domain = Diebold.com
    > -----------------------------------
    > "Yama" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> How can I dynamically embed the content of a filename.js into a .aspx

    > page
    >> before rendering it to the client?
    >>
    >> Thanks,
    >>
    >> ~yamazed
    >>
    >>

    >
    >
     
    Yama, Oct 22, 2005
    #7
  8. Yama

    Yama Guest

    Hey Ken,

    I am going to grab some of ya code dude... And I thought I was a smarty
    one! LOL I love your handler.

    Thanks for sharing (my blogger will soon be up)

    ~yamazed


    "Ken Varn" <nospam> wrote in message
    news:%23n%...
    > See my codeproject article at
    >
    > http://www.codeproject.com/aspnet/DynamicContentRendering.asp
    >
    > There is a class in the project called EmbeddedJScript. It basically
    > wraps
    > a bunch of .NET functions that allow you to embed a JavaScript file as a
    > resource and then dynamically render it to the page.
    >
    > --
    > -----------------------------------
    > Ken Varn
    > Senior Software Engineer
    > Diebold Inc.
    >
    > EmailID = varnk
    > Domain = Diebold.com
    > -----------------------------------
    > "Yama" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> How can I dynamically embed the content of a filename.js into a .aspx

    > page
    >> before rendering it to the client?
    >>
    >> Thanks,
    >>
    >> ~yamazed
    >>
    >>

    >
    >
     
    Yama, Oct 22, 2005
    #8
  9. Yama

    Yama Guest

    Hi Ken,

    Everything is working fine for me; however, I cannot figure out a way to
    cache the javascript it seems like on every page refresh it reads from the
    javascript file. Any ideas how to cache it?

    Thanks,

    Yama



    "Ken Varn" <nospam> wrote in message
    news:%23n%...
    > See my codeproject article at
    >
    > http://www.codeproject.com/aspnet/DynamicContentRendering.asp
    >
    > There is a class in the project called EmbeddedJScript. It basically
    > wraps
    > a bunch of .NET functions that allow you to embed a JavaScript file as a
    > resource and then dynamically render it to the page.
    >
    > --
    > -----------------------------------
    > Ken Varn
    > Senior Software Engineer
    > Diebold Inc.
    >
    > EmailID = varnk
    > Domain = Diebold.com
    > -----------------------------------
    > "Yama" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> How can I dynamically embed the content of a filename.js into a .aspx

    > page
    >> before rendering it to the client?
    >>
    >> Thanks,
    >>
    >> ~yamazed
    >>
    >>

    >
    >
     
    Yama, Oct 22, 2005
    #9
  10. Yama

    Ken Varn Guest

    There may be one other way that I have been trying. I have made some
    modifications to the code that I published on CodeProject, but I have not
    re-submitted it yet. I basically substituted the HttpModule with an
    HttpHandler instead. The Handler then can recall the custom data from
    server memory without having to re-load it on each postback. The event
    handlers are stored in session state, so they do not have to be destroyed on
    each postback as it is done on the current implementation. So far I have
    only done it on the CustomImage controls, but I don't see why it couldn't be
    done on the JavaScript custom handler as well. If you want the source I can
    send it to you.


    --
    -----------------------------------
    Ken Varn
    Senior Software Engineer
    Diebold Inc.

    EmailID = varnk
    Domain = Diebold.com
    -----------------------------------
    "Yama" <> wrote in message
    news:Ozj%...
    > Hi Ken,
    >
    > Everything is working fine for me; however, I cannot figure out a way to
    > cache the javascript it seems like on every page refresh it reads from the
    > javascript file. Any ideas how to cache it?
    >
    > Thanks,
    >
    > Yama
    >
    >
    >
    > "Ken Varn" <nospam> wrote in message
    > news:%23n%...
    > > See my codeproject article at
    > >
    > > http://www.codeproject.com/aspnet/DynamicContentRendering.asp
    > >
    > > There is a class in the project called EmbeddedJScript. It basically
    > > wraps
    > > a bunch of .NET functions that allow you to embed a JavaScript file as a
    > > resource and then dynamically render it to the page.
    > >
    > > --
    > > -----------------------------------
    > > Ken Varn
    > > Senior Software Engineer
    > > Diebold Inc.
    > >
    > > EmailID = varnk
    > > Domain = Diebold.com
    > > -----------------------------------
    > > "Yama" <> wrote in message
    > > news:...
    > >> Hi,
    > >>
    > >> How can I dynamically embed the content of a filename.js into a .aspx

    > > page
    > >> before rendering it to the client?
    > >>
    > >> Thanks,
    > >>
    > >> ~yamazed
    > >>
    > >>

    > >
    > >

    >
    >
     
    Ken Varn, Oct 28, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Whitney
    Replies:
    3
    Views:
    391
    Louis Dascoulias
    Jul 17, 2003
  2. Rob Heckart
    Replies:
    6
    Views:
    550
    Rob Heckart
    Nov 26, 2003
  3. hazz
    Replies:
    6
    Views:
    49,773
    SkyUCHC
    Jun 9, 2010
  4. Peter Mount
    Replies:
    4
    Views:
    975
    Peter Mount
    Jan 31, 2006
  5. =?Utf-8?B?R1RC?=

    Content Not Rendering in Master Page

    =?Utf-8?B?R1RC?=, Jul 25, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    634
    Craig
    Sep 27, 2006
Loading...

Share This Page