最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - Javascript pageLoad not executing in a master page - Stack Overflow

programmeradmin2浏览0评论

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>

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>
Share Improve this question edited Feb 3 at 2:46 Pang 10.1k146 gold badges86 silver badges124 bronze badges asked Dec 9, 2009 at 16:57 JeremyJeremy 9,33322 gold badges59 silver badges70 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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>

Window Load Event

window.onload = function() {
    alert('pageLoad'); 
}

Self Executing Function

(function() {
    alert('pageLoad'); 
})();
发布评论

评论列表(0)

  1. 暂无评论