Reader Kevin posed a good question in response to my last post, the answer to which is useful enough to warrant a separate post:
I have a situation where I call a page load handler from a script associated with a master page, then on a specific page based on that master page I need to call a second page load handler. I use the pageLoad() shortcut notation in the master page script. I can’t get either pageLoad() (expected since the composite page would have two pageLoad’s) or $addHandler(document, “load�, mySpecificPageLoad()) to work. I had to resort to my old “addLoadListener� function to add the second page load event handler. Is this as expected?
As he mentions, there’s only room in the page’s DOM for one pageLoad shortcut function, preventing both a master and content page from utilizing the shortcut simultaneously.
To work around this limitation, simply add this to your master’s pageLoad:
<script type="text/javascript">
function pageLoad() {
// Master pageLoad() code.
// If function contentPageLoad exists, execute it.
if(typeof contentPageLoad == 'function')
contentPageLoad();
}
Now, contentPageLoad is executed on any content page you wish to include it on, just as if it were pageLoad. If you don’t include it on a particular content page, the conditional will prevent its execution (and subsequent JavaScript error).
You could use Sys.Application.add_Load to wire up a custom pageLoad on every single content page, but I think the DRY-ness of this approach is preferable. The above method also allows you to retain control of the order of execution, through the placement of the contentPageLoad call in pageLoad.