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
}

5 Responses to “JavaScript pageLoad() for MasterPage and ContentPage”

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

    [...] 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. JavaScript pageLoad() for MasterPage and ContentPage « mgthantzin.com/blog Says:

    [...] Credit goes to devioblog. [...]

Leave a Reply