admin管理员组文章数量:1394971
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...
- 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?
- 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...
- 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?
- Is this a known bug or a behavior we will need to take into account from now on?
-
2
I guess
var onload
actually setswindow.onload
. – Sebastian Simon Commented Aug 13, 2015 at 6:07
3 Answers
Reset to default 5You'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);
})()
本文标签: javascriptMicrosoft Edge calling onload named function on page loadingStack Overflow
版权声明:本文标题:javascript - Microsoft Edge calling onload named function on page loading - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744112636a2591359.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论