RegisterStartupScript and where it's placed in the page

Last post 04-27-2009 3:49 PM by pranaysharmadelhi. 7 replies.

Sort Posts:

  • RegisterStartupScript and where it's placed in the page

    08-06-2008, 8:52 AM
    • Member
      2 point Member
    • Rob.Moran
    • Member since 06-03-2008, 9:53 PM
    • Posts 18

    I've been banging my head for a while now and before I go mad, I was wondering if anyone can help me out.

    I need to run some client-side javascript after a page has loaded and after specific postbacks are completed.

    To undertake this, I am using Page.ClientScript.RegisterStartupScript(...) which I believe is the correct way.

    The problem is, it needs to run functions held in external scripts referenced by the page (including the ajax framework - I'm using scriptpath to serve this from a seperate domain).

    With the loadscriptsbeforeui attribute of the scriptmanager set to false (best practice to load scripts at the end of the document), the external scripts appear *AFTER* the script added by RegisterStartupScript meaning required functions are not yet available.

    Is there a way to get RegisterStartupScript scripts to be the last script in the page?

    Kind regards,


    Rob

  • Re: RegisterStartupScript and where it's placed in the page

    08-06-2008, 9:29 AM
    • Contributor
      4,775 point Contributor
    • Adam.Kahtava
    • Member since 10-18-2006, 2:14 PM
    • Canada
    • Posts 927

    You can use  Page.ClientScript.RegisterClientScriptBlock(Type, String, String).

    "The RegisterClientScriptBlock method inserts the client-side script immediately below the opening tag of the Page object's <form runat="server"> element. The code cannot access any of the form's elements because, at that time, the elements haven't been instantiated yet. This explains why hdnView variable had a null value in my case. The RegisterStartupScript method inserts the specified client-side script just before the closing tag of the Page object's <form runat="server"> element. The code can access any of the form's elements because, at that time, the elements have been instantiated. The choice of which method to use really depends on the "order" in which you want your script to be run by the browser when rendering the page."

    See this article for more information: RegisterClientScriptBlock and RegisterStartupScript

  • Re: RegisterStartupScript and where it's placed in the page

    08-06-2008, 10:34 AM
    • Member
      2 point Member
    • Rob.Moran
    • Member since 06-03-2008, 9:53 PM
    • Posts 18

    RegisterClientScriptBlock seems to put it in the page earlier (after the opening <form> tag).

    e.g.:

     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >

    <body>    
    <form name="aspnetForm" method="post" action="test.aspx" id="aspnetForm">

    <script type="text/javascript">
    //<![CDATA[
    alert('RegisterClientScriptBlock is here');//]]>
    </script>


    <script src="http://localhost/evince/evince.WebApplication/_Resource/script/ajax/System.Web.Extensions/3.5.0.0/3.5.21022.8/MicrosoftAjax.js" type="text/javascript"></script>
    <script src="http://localhost/evince/evince.WebApplication/_Resource/script/ajax/System.Web.Extensions/3.5.0.0/3.5.21022.8/MicrosoftAjaxWebForms.js"

    [PAGE CONTENTS HERE]


    <script type="text/javascript">
    //<![CDATA[
    alert('RegisterStartupScript is here');//]]>
    </script>


    <script src="http://localhost/evince/evince.WebApplication/_Resource/script/ajax/AjaxControlToolkit/3.0.20229.20843/3.0.20229.0/AjaxControlToolkit.ExtenderBase.BaseScripts.en.js" type="text/javascript"></script>
    <script src="http://localhost/evince/evince.WebApplication/_Resource/script/ajax/AjaxControlToolkit/3.0.20229.20843/3.0.20229.0/AjaxControlToolkit.Common.Common.js" type="text/javascript"></script>
    <script src="http://localhost/evince/evince.WebApplication/_Resource/script/ajax/AjaxControlToolkit/3.0.20229.20843/3.0.20229.0/AjaxControlToolkit.Common.DateTime.js" type="text/javascript"></script>
    ...

    [I NEED JAVASCRIPT HERE]

    <script type="text/javascript">
    //<![CDATA[
    Sys.Application.initialize();
    //]]>
    </script>
    </form>
    </body>
    </html>

     

  • Re: RegisterStartupScript and where it's placed in the page

    08-06-2008, 10:55 AM
    • Contributor
      4,775 point Contributor
    • Adam.Kahtava
    • Member since 10-18-2006, 2:14 PM
    • Canada
    • Posts 927

    What I think you're saying is: you need a piece of JavaScript to be loaded after everything else has loaded?

    Could you assign your JS to an event and then have it fired after the window finished loading? Then you're not as dependent on loading order.

    Example:

      window.onload = function() { alert('hello page load'); };

  • Re: RegisterStartupScript and where it's placed in the page

    08-07-2008, 8:23 AM
    • Member
      2 point Member
    • Rob.Moran
    • Member since 06-03-2008, 9:53 PM
    • Posts 18

     That's right, but the script also needs to run after a postback has happened not just on the initial load.

     Cheers,

     Rob

  • Re: RegisterStartupScript and where it's placed in the page

    08-07-2008, 8:33 AM
    Answer
    • Contributor
      4,775 point Contributor
    • Adam.Kahtava
    • Member since 10-18-2006, 2:14 PM
    • Canada
    • Posts 927

    The window.onload is a JavaScript event and will be fired every time the browser window is loaded - it will fire on post-backs and the initial load.

  • Re: RegisterStartupScript and where it's placed in the page

    08-07-2008, 8:46 AM
    • Member
      2 point Member
    • Rob.Moran
    • Member since 06-03-2008, 9:53 PM
    • Posts 18

     Thanks, I'll take a look

  • Re: RegisterStartupScript and where it's placed in the page

    04-27-2009, 3:49 PM

    Here is the correct way of doing it:

            String scriptString = @"Sys.Application.add_load(applicationLoadHandler);         
                                    function applicationLoadHandler()
                                    {
    //Write your code in Javascript here to be executed in the end.
    alert('Executed in end');

                                    }";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "anchor", scriptString, true);

    Seems to work off both IE and Firefox

     

Page 1 of 1 (8 items)