14

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.

Onur Kucukkece
  • 1,708
  • 1
  • 20
  • 46
  • 1
    Why not try [`$(document).ready()`](http://api.jquery.com/ready/) instead? – Code Maverick Apr 01 '13 at 15:38
  • 1
    Hey @Scott, $(document).ready() fires when the dom is loaded but I want the function to be fired when all the content including images etc.. is loaded – Onur Kucukkece Apr 01 '13 at 16:56
  • 1
    @Onur - Ok, then [`$(window).load`](http://api.jquery.com/load-event/) is what you want. I would create a [jsFiddle](http://jsFiddle.net) to test what you want. If it works there, then most likely the problem would be in your markup or another script that interfering. – Code Maverick Apr 01 '13 at 17:16
  • @Scott you're right, I've just found the problem, another script called jquery vegas is interfering with this function. Thank you for your help. – Onur Kucukkece Apr 01 '13 at 17:24

4 Answers4

9

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
});
Onur Kucukkece
  • 1,708
  • 1
  • 20
  • 46
2

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

<script type="text/javascript">
bandolero
  • 131
  • 1
  • 7
1

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?

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • 1
    If your `page` has more than 1 `window.load` `function` then only 1 will work. – Rohan Kumar Apr 01 '13 at 15:42
  • I have only 1 window.load function, and $(document).ready() is neing fired when the dom is loaded but I want the function to be fired when all the content including images etc.. is loaded – Onur Kucukkece Apr 01 '13 at 16:57
0

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>
Saeed Afshari
  • 939
  • 1
  • 9
  • 17