window.onload function doesn't work on Mozilla Firefox

别说谁变了你拦得住时间么 提交于 2019-12-08 17:23:08

问题


I'm using a loading screen for a webpage and I use window.onload function.

Everything works great except in Mozilla Firefox browsers. When we first visit or refresh the page with ctrl+F5 combination, the loading screen never disappears. if we refresh the page only with F5, then it works.

I use the code below

$(window).load(function(e) {
    $("#body-mask").fadeOut(1000,function(){
        $(this).remove();
    });
});

I have also tried the code below but nothing changed.

window.onload = function () {
   $("#body-mask").fadeOut(1000,function(){
       $(this).remove();
   });
}

Why this is happening?

Please help.

Thanks in advance.


回答1:


The problem is caused by another jquery background plugin which is placed inside $(document).ready()

I moved it inside $(window).load() function, now it works perfect.

I have also moved another function to resize images on the page load. When it was inside $(document).ready() block, sometimes it was malfunctioning if loading time took too long but now it also works great.

function resizeImages(){
    //Some Code
}

$(window).load(function(){
    $("#body-mask").fadeOut(1000,function(){
        $(this).remove();
    });

    $.vegas({
        src: backURL , fade:0
    });

    resizeImages();
});

$(document).ready(function(){
    //Some Other code
});



回答2:


Try This:

$(document).ready(function(e) {
    $("#body-mask").fadeOut(1000,function(){
        $(this).remove();
    });
});

Read for load and ready functions difference What is the difference between $(window).load and $(document).ready?




回答3:


You must call function on initialization like :

window.onload = init();

in other word modify your code to:

window.onload = function () {
$("#body-mask").fadeOut(1000,function(){
    $(this).remove();
});
}();// Added

Copy following code in file then open it with firefox

<script>
window.onload = function () {
alert('saeed')
}();
</script>



回答4:


I got the same problem when mistyped the type attribute in the script tag:

<script type="text/javascript">


来源:https://stackoverflow.com/questions/15746450/window-onload-function-doesnt-work-on-mozilla-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!