managing document.ready event(s) on a large-scale website

后端 未结 9 1455
予麋鹿
予麋鹿 2021-01-30 10:51

NOTE: I have now created a jQuery plugin which is my attempt of a solution to this issue. I am sure that it could be improved and i\'ve probably overlooked lots of use c

9条回答
  •  天涯浪人
    2021-01-30 11:51

    Great question. I actually handle this by using custom page load events. So in my core .js file I have a class like the following:

    var Page = {
        init: function(pageName) {
            switch (pageName)
            {
                case: 'home': {
                    // Run home page specific code
                }
                case: 'about': {
                    // Run about page specific code
                }
                ...
            }
        }
    }
    

    You can call this a bunch of ways, either in a page-specific $(document).ready() or from the core script using some kind of URL parser (literally anything is possible with a URL parser):

    $(document).ready(function() {
        // http://www.mydomain.com/about
        var currentPage = window.location.pathname;   // "about"
        Page.init(currentPage);    
    });
    

    window.location DOM reference: https://developer.mozilla.org/en/DOM/window.location

提交回复
热议问题