Overriding Window = Onload
I have a page within wordpress that I want to password protect via a user role plugin. Everything works fine on straight forward pages but I have a page with window.onload = functi
Solution 1:
You have to use addEventListener
or attachEvent
to load multiple functions. If you want to use window.onload = ..
, use the code in the last else
block at the function below:
functionaddEvent(name, func) {
if (window.addEventListener) {
window.addEventListener(name, func, true);
} elseif(window.attachEvent) {
window.attachEvent('on' + name, func);
} else {
var other_func = typeofwindow['on'+name] == "function" ? window['on'+name] : function(){};
window['on' + name] = function(ev){
func(ev);
other_func(ev);
}
}
}
addEvent('load', function(){
//Load function
});
Solution 2:
Instead of assigning it directly to the onload
property add it as an event listener
https://developer.mozilla.org/en/DOM/element.addEventListener
You'll need to use attachEvent
for IE versions < 9.
http://msdn.microsoft.com/en-us/library/ms536343(v=vs.85).aspx
If you're using a framework such as jQuery or Prototype this can be abstracted out so you don't need to worry about different browsers.
Post a Comment for "Overriding Window = Onload"