Overriding window = onload

2019-06-28 06:38发布

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 = function() { that completely overrides the password function.

I want the page to load immediately after it's checked to see if the user is logged in or not.

Update:

I'm using this plugin and I just have the function:

<script type="text/javascript"> 
(function() { 

window.onload = function() { 
 var map = new google.maps.Map(document.getElementById('map'), options);
...
} } )
</script>

Which then loads on this div:

<div id="map" style="width:100%; height:100%"></div>

2条回答
趁早两清
2楼-- · 2019-06-28 06:58

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-06-28 07:12

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:

function addEvent(name, func) {
    if (window.addEventListener) {
        window.addEventListener(name, func, true);
    } else if(window.attachEvent) {
        window.attachEvent('on' + name, func);
    } else {
        var other_func = typeof window['on'+name] == "function" ? window['on'+name] : function(){};
        window['on' + name] = function(ev){
            func(ev);
            other_func(ev);
        }
    }
}

addEvent('load', function(){
   //Load function 
});
查看更多
登录 后发表回答