I’m refactoring about 600 lines of javascript into the module pattern. Here is a start from previous post:
I undertand the concept of anonymous methods…and sefl-executing….but not the scoping concepts…i.e. what global and window do.
window.onload=initialize_page;
(function (global) {
global['test'] = 'test';
function initialize_page()
{
/* fill here */
}
})(window);
Can I put window.onload=initialize_page into my module pattern? Or does it need to be put outside of it? Can someone explain how the access works?
EDIT 1: per Answer
(function () {
addEventListener('load', initialize_page);
function initialize_page()
{
alert ("hi");
}
})();
It is a global. Don’t touch it like that, you’ll overwrite any other code that tries to assign load handlers.
Use
addEventListener(orattachEventfor old IE) instead. There are plenty of libraries that abstract the functionality.