Javascript pre-loader not waiting for images - javascript

I found this Javascript to activate my website's pre-loader. However, it seems to disappears once the page has loaded and not when images have finished loading.
After searching I found someone suggesting window.onload which waits until images have loaded, but I can't seem to figure out how to implement it into my existing Javascript.
$(document).ready(function(){
$(".se-pre-con").fadeOut("slow")

If you would like to use the window.onload function to hide your preloader, you can attach to the event in jQuery using the following snippet:
$(window).on('load', function () {
$(".se-pre-con").fadeOut("slow");
}):
Make sure to use the .on() method signature and not the event signature .load(), which was deprecated in jQuery 1.8 and removed in jQuery 3.0.
You should note the following from the .load() docs:
Caveats of the load event when used with images
A common challenge developers attempt to solve using the .load() shortcut is to execute a function when an image (or collection of images) have completely loaded. There are several known caveats with this that should be noted. These are:
It doesn't work consistently nor reliably cross-browser
It doesn't fire correctly in WebKit if the image src is set to the same src as before
It doesn't correctly bubble up the DOM tree
Can cease to fire for images that already live in the browser's cache

Related

Run javascript after gif is fully loaded with jQuery

I know I can use
$(".container").ready(function(){
// Run after DOM loaded
});
to run code after the DOM has been fully loaded. This includes images though it seems it doesn't include GIF-images. From what I can see, loading the first frame of the gif counts as "loaded".
Is there a way to trigger code after the fill gif has been loaded?
The <img> element has on onload event - this should do the trick.
If you'd like to use jQuery for that, you can read about it here: https://api.jquery.com/load-event/
Images have a onload event that fires.
$(".image-selector").on("load", function(){
// Run after the image has loaded.
});
If you are targetting an older version of IE (pre IE9) there are sometimes troubles if dynamically changing the src property and you'll also have to check the image.complete property.
Edit: Went looking for the property name and found this question: jQuery callback on image load (even when the image is cached)
You may use the imagesloaded plugin for this use case.
How about this
$(document).ready(function(){
$("gif selector").load(function(){
alert("gift loaded.");
});
});
Example: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_event_load

Accessing controls early | load vs. domready

I was on jsfiddle.net recently and I saw this as a configuration option. This got me to thinking that it might help a problem I'm having as such:
I load multiple images (haven't upgraded to a single sprite yet) so that I can not use my controls until they are all downloaded...the images take most of the download time so that for the first few seconds I can not access my controls.
Currently I'm using one of these two..both work.
window.onload = initialize_page
window.addEventListener('load',initialize_page);
Related
Jquery document ready vs. window.onload
window.onload vs. body.onload vs. document.onready
window.onload vs <body onload=""/>
AFAIK onDomReady() fires once the DOM has loaded. If the page contains external sources, such as images, then it may fire before these have finished loading.
onLoad() fires after the whole page has finished loading, including external sources.
So it's possible for onDomReady() to fire before onLoad() but you'll have to test it on your page.

How to call function after document ready AND image load?

I was using something like this:
$(document).ready(function() {
$('#my-img').load(function() {
// do something
});
});
But sometimes it fails to execute the second callback (without throwing any errors, so there's nothing to do there), and I'm thinking that maybe the image is being loaded before the document is ready.
If I don't use the $(document).ready() part, it works fine, so I think I'm gonna leave it that way for now. But someone told me that it was a good practice to always do this kind of thing as a callback on document ready, because the document might not be ready. Is that right?
Any thoughts?
Taken from the documentation on load()
Caveats of the load event when used with images
A common challenge developers attempt to solve using the .load()
shortcut is to execute a function when an image (or collection of
images) have completely loaded. There are several known caveats with
this that should be noted. These are:
It doesn't work consistently nor reliably cross-browser
It doesn't fire correctly in WebKit if the image src is set to the same src as before
It doesn't correctly bubble up the DOM tree
****Can cease to fire for images that already live in the browser's cache****
Especially the latter is a common problem.
You might try the imagesLoaded plugin, but I've had better luck with the following approach:
var element = $('#my-img');
$("<img/>").load(function () { //create in memory image, and bind the load event
//do something
//var imageHeight = this.height;
}).attr("src", element.attr("src"));
//set the src of the in memory copy after binding the load event, to avoid WebKit issues
It's dirty, but if you really need to perform some action after the image has loaded this has been the only reliable approach I've been able to get to work.
Old question but may be useful to googlers: If you need code to execute after images are loaded, and the images are in the DOM, you should use $(window).load instead of $(document).ready. As in:
$(window).load(function() {
console.log("My image is " + $('#my-img').width() + " pixels wide.");
// You may not have known this before the image had loaded
});
This is vital if doing any jiggerypokery with SVGs or other embedded documents.

Is there a reason why ('img').load doesn't work (jQuery)?

I have a set of images that I wanted to load first before executing the next line of code. The code is like:
$('div.some_class img').load(function() {
// do something to div.some_class img
});
However, the images do not load before executing the commands. When I try (window).load though, it works.
My question is, why doesn't ('div.some_class img').load work? Or is it because I'm using a class selector which is the slowest way to select an element?
from http://api.jquery.com/load-event/
Caveats of the load event when used with images
A common challenge developers attempt to solve using the .load()
shortcut is to execute a function when an image (or collection of
images) have completely loaded. There are several known caveats with
this that should be noted. These are:
It doesn't work consistently nor reliably cross-browser
It doesn't fire correctly in WebKit if the image src is set to the same src as before
It doesn't correctly bubble up the DOM tree
Can cease to firefor images that already live in the browser's cache
Hope this answers your question.
https://gist.github.com/268257 - this is a jQuery plugin to check for all images loaded
I don't think that load checks for the image to have actually loaded as much as that the tag has loaded? I had the same problem so used the above plugin

Only load HTML into an IFRAME

How can I only load the HTML of a page into an IFRAME, without automatically triggering the download of all the css,scripts,images,videos on the page?
Or can you get an event the moment the DOM is "ready".. when the initial HTML has loaded and nothing much more.
To get only the HTML contents, try using an AJAX call. That would, of course, return the content in a variable, but you might process it as you see fit afterward.
There is no cross-browser way to do this. Some browsers provide events that fire when the DOM loads, like DOMContentLoaded for Gecko.
jQuery implements this functionality. I suggest you either use jQuery for this:
$(document).ready(function () {
// Function is called when the DOM is loaded.
});
Or check how jQuery implements it. See the bindReady function in the jQuery source code.
The short answer is "You can't".
Internet Explorer supports a propriatry attribute which can prevent scripts from executing, but it isn't cross browser and doesn't deal with images or stylesheets.
A number of JS libraries implement a custom event that fires when the DOM is ready - but browsers load resources in parallel, so while that event may fire before all images and stylesheets are loaded, it is unlikely to fire before the other elements start to download.
If you really want the page to load without those things - process it on the server to strip out the HTML that includes them.
Set 'Content-type' for that HTML page to text/plain.

Categories

Resources