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 thoughts on “JavaScript pageLoad() for MasterPage and ContentPage

  1. Pingback: JavaScript pageLoad() for MasterPage and ContentPage « devioblog

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

  3. 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. Pingback: JavaScript pageLoad() for MasterPage and ContentPage «

  5. Hi,

    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. Pingback: - JavaScript pageLoad() for MasterPage and ContentPage

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.