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

javascript - Microsoft Edge calling onload named function on page loading - Stack Overflow

programmeradmin5浏览0评论

A simple html like this one above, opened on Edge ,would show the alert.

<html>
    <head>
        <script>
        var onload = function() {
            alert('I\'ve been called');
        };
        </script>
    </head>
    <body> my body </body>
</html>

More, if you add window.addEventListener('load', onload, false); to your code, the function get called twice..

I have a web app and my onload functions are called twice on everypage...

  1. Would there be a way to disable this feature, server side so the function would only be called once without any need to refactor all my code?
  2. Is this a known bug or a behavior we will need to take into account from now on?

A simple html like this one above, opened on Edge ,would show the alert.

<html>
    <head>
        <script>
        var onload = function() {
            alert('I\'ve been called');
        };
        </script>
    </head>
    <body> my body </body>
</html>

More, if you add window.addEventListener('load', onload, false); to your code, the function get called twice..

I have a web app and my onload functions are called twice on everypage...

  1. Would there be a way to disable this feature, server side so the function would only be called once without any need to refactor all my code?
  2. Is this a known bug or a behavior we will need to take into account from now on?
Share Improve this question asked Aug 13, 2015 at 6:01 BenoîtBenoît 15k7 gold badges64 silver badges88 bronze badges 1
  • 2 I guess var onload actually sets window.onload. – Sebastian Simon Commented Aug 13, 2015 at 6:07
Add a ment  | 

3 Answers 3

Reset to default 5

You're assigning a function to window.onload AND you're installing an event listener to the window load event. You are hooking up two event listeners to the same function so it is getting called twice.

Remember, global variables are all properties of the window object too so when you do:

var onload = function() {...}

in the global namespace, that is identical to doing:

window.onload = function() {...}

Which is configuring an event handler.

If you don't want this problem, then change the name of your onload function to something that doesn't conflict with window.onload or use an anonymous function (this is a great example of why you should use an anonymous event handler whenever possible and avoid using the global namespace whenever possible).


I would suggest either this:

// anonymous function
window.addEventListener('load', function() {
    alert('I\'ve been called');
}, false);

or this:

// use IIFE function to insulate from global namespace
(function() {
    var onload = function() {
        alert('I\'ve been called');
    };
    window.addEventListener('load', onload, false);
})();

There is nothing related to microsoft edge. It's just ES6 that is implemented into edge, and not yet in other browsers. It's not a bug, it's a feature.

So, your code will break in chrome, firefox too within few weeks.
https://thechamplord.wordpress./2014/07/04/using-javascript-window-onload-event-properly/
https://msdn.microsoft./en-us/library/cc197055%28v=vs.85%29.aspx

Also, why would you give a function with such a name?

There is no way to disable this. Avoid polluting the global namespace like that and simply wrap your function in an IIFE. It will help with garbage collection as well for other scenarios.

(function(){
    var onload = function() {
        alert('I\'ve been called');
    };
    window.addEventListener('load', onload, false);
})()
发布评论

评论列表(0)

  1. 暂无评论