JavaScript pageLoad() for MasterPage and ContentPage

I was experimenting with the DragPanelExtender recently, and almost immediately ran into the question of how to position it upon opening.

There are a lot of articles to be found on this question, and they always deal with it by means of a JavaScript function called pageLoad(), which is called by the ASP.Net Ajax framework sometime while the page is loading.

Now my application already has a pageLoad() function in the MasterPage, so adding a function of the same name in the ContentPage just did not work, as it would not be called by the framework.

My knowledge of JavaScript is not too profound, and I found that a function definition in JavaScript is actually a property of the window object. The hint was a line in the MicrosoftAjax.js file:

if (window.pageLoad) window.pageLoad(this,a);

meaning: if there is a defined function named pageLoad(), it is a property of the window object, and the value of the property is not null (== not false), and I can call it.

The solution was clear to me soon: if I need a pageLoad() method in the ContentPage, I need to name it differently and check for its definition in the MasterPage.

The pageLoad() in the MasterPage looks like this:

function pageLoad(sender, args)
    // ... do my MasterPage JavaScript stuff...
    if (window.contentPageLoad) {
        window.contentPageLoad(sender, args);

and the ContentPage has its equivalent contentPageLoad() function:

function contentPageLoad(sender, args)
    // do something

11 Responses to JavaScript pageLoad() for MasterPage and ContentPage

  1. […] Read the original here:  JavaScript pageLoad() for MasterPage and ContentPage « devioblog […]

  2. CMS says:

    Thanks for the post. Thanks to your post I resolved a problem with multiple update panels in both MasterPage and conten pages.

  3. Tim says:

    I also want to thank you for this posting. It was a great help and something that I would surely not have figured out on my own!

  4. Thant Zin says:

    Thanks for this post. It really works and saves my time.😉

  5. Glenn says:


    Thankyou for this post!

    It’s worth noting that Web User Controls that have their own pageLoad() functions defined can cause this same behaviour (ie. pageLoad() in a normal ASP.Net page failing to be fired).

    The solution is the same.. define a separate (differently named) function in your actual web page that is called by the pageLoad() function in your Web User Control.

  6. Saulo says:

    You just saved me! Thanks!

  7. Mohammed says:

    thanks so much for this post really you saved my time

  8. Sandeep says:

    Amazing article….!…Too good…saves hell lot of time…

  9. Seb says:

    An alterantive and perhaps more scalable approach, if you’re using ASP.Net, is to register the startup script using ClientScript.RegisterStartupScript or ToolkitScriptManager).RegisterStartupScript, as described here:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: