javascriptasp.netdom-eventsmaster-pages

Javascript pageLoad not executing in a master page


What causes the following pageLoad to not execute in a master page? The first alert executes as expected, but the pageLoad function is not being invoked. How can I use the function within a master/content page?

<script type="text/javascript">
        alert('test');
        function pageLoad(sender, args) {
            alert('pageLoad');
        }    
</script>

Solution

  • I'm assuming a script manager control is present in your master page? I'm not sure if the client-side pageLoad method is only called on the initial page load. I do know that if you're looking for ajax postback page load hooks, try the client-side PageRequestManager. MAke sure that the code below is place inside the script manager.

        <asp:ScriptManager ID="ScriptManager1" runat="server" />
    
        <script type="text/javascript" >
        (function() {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
    
            if (prm)
            {
                prm.add_endRequest(
                function (sender, args) {            
                    // do your stuff
    
                    // Handle a weird occurence where an error is returned but the error code is 0, i.e. no error.
                    if(args.get_error() && args.get_error().name === 'Sys.WebForms.PageRequestManagerServerErrorException')
                    {
                        args.set_errorHandled(args._error.httpStatusCode == 0);
                    }
                });
            }
        })();
        </script>
    
    <%-- Other markup --%>
    </asp:ScriptManager>