admin管理员组文章数量:1323716
I'm facing a weird issue. The console.log() outside the onload function works, but the console.log() inside doesn't work... Would it mean that my page never fully loads ? I had a look at the developer tools of Chrome and it shows me that the page is loaded, so I don't really understand... (here is a screen of the devtool)
Here is my code:
console.log("hello1");
window.onload = function()
{
console.log("hello2");
};
(I'm using this in a WordPress website, but I don't think it changes anything)
Thanks in advance,
ArbreMojo.
I'm facing a weird issue. The console.log() outside the onload function works, but the console.log() inside doesn't work... Would it mean that my page never fully loads ? I had a look at the developer tools of Chrome and it shows me that the page is loaded, so I don't really understand... (here is a screen of the devtool)
Here is my code:
console.log("hello1");
window.onload = function()
{
console.log("hello2");
};
(I'm using this in a WordPress website, but I don't think it changes anything)
Thanks in advance,
ArbreMojo.
Share Improve this question edited Dec 4, 2018 at 14:14 nicholaswmin 23k16 gold badges101 silver badges173 bronze badges asked Dec 4, 2018 at 13:58 ArbreMojoArbreMojo 992 silver badges9 bronze badges 3- jsfiddle/7opyw5h4 - it works fine by itself. Possibly something else has overridden the handler. If you're using something like WordPress there might be all kinds of scripts being loaded by the framework. – ADyson Commented Dec 4, 2018 at 14:02
-
One other possibility is that this code is actually inside of another piece of code that runs after the
load
event fires. (As one example, if this were inside of aclick
event handler, and you did the click long after the page loaded.) – apsillers Commented Dec 4, 2018 at 14:03 - Thank you for your answers guys :) I don't think this code is inside of another piece of code, but it's possible that some other scripts of WordPress override the handler, as ADyson said it. I will continue to search :) Thanks a lot again ! – ArbreMojo Commented Dec 4, 2018 at 14:08
1 Answer
Reset to default 9Some other code is probably assigning another function value to the window.onload
method, so it basically overrides your assignment.
Instead of window.onload = function
you can do:
window.addEventListener('load', function() {
console.log('loaded')
})
which allows attaching an arbitrary number of handlers for that event. This ensures nothing can override your callback function.
See: EventTarget.addEventListener for more info.
本文标签: javascriptwindowonload function not runningStack Overflow
版权声明:本文标题:javascript - window.onload function not running - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126144a2421949.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论