jQuery window.onload 函数在 Mozilla Firefox 上不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15746450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:44:20  来源:igfitidea点击:

window.onload function doesn't work on Mozilla Firefox

javascriptjqueryfirefoxonloadmozilla

提问by Onur Kucukkece

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

我正在为网页使用加载屏幕,并使用 window.onload 函数。

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.

除了在 Mozilla Firefox 浏览器中,一切都很好。当我们第一次访问或使用 ctrl+F5 组合刷新页面时,加载屏幕永远不会消失。如果我们只用 F5 刷新页面,那么它就可以工作。

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.

提前致谢。

采纳答案by Onur Kucukkece

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

该问题是由另一个 jquery 后台插件引起的,该插件放置在 $(document).ready() 中

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

我将它移到 $(window).load() 函数中,现在它完美运行。

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.

我还移动了另一个功能来在页面加载时调整图像大小。当它在 $(document).ready() 块中时,如果加载时间太长,有时它会出现故障,但现在它也很好用。

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
});

回答by bandolero

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

在脚本标签中输入错误类型属性时,我遇到了同样的问题:

<script type="text/javascript">

回答by Saeed Afshari

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

将以下代码复制到文件中,然后用 Firefox 打开它

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

回答by Rohan Kumar

Try This:

尝试这个:

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

Read for loadand readyfunctions differenceWhat is the difference between $(window).load and $(document).ready?

阅读loadreadyfunctions difference$(window).load 和 $(document).ready 有什么区别?