Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery 3.0 $(window).load(function(){});

Tags:

jquery

so jQuery 3.0 was released today, and for some reason the following code no longer works on my site:

$(window).load(function() {}); 

Can anyone suggest how I fix this or an alternative that actives when /everything/ is loaded?

like image 769
Matt Cowley Avatar asked Jun 10 '16 14:06

Matt Cowley


People also ask

What is the difference between $( window .load and $( document .ready function in jQuery?

ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc. are loaded, but after the whole DOM itself is ready.

What is window onload in jQuery?

The code which gets included inside $( window ). on( "load", function() { ... }) runs only once the entire page is ready (not only DOM). Note: The load() method deprecated in jQuery version 1.8. It was completely removed in version 3.0.

How can we call a method on page load using jQuery?

$( document ). ready() ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute. Code included inside $( window ). on( "load", function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready. // A $( document ).

What is jQuery load function?

The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element.


1 Answers

Reading from breaking-change-load-unload-and-error-removed:

Breaking change: .load(), .unload(), and .error() removed

These methods are shortcuts for event operations, but had several API limitations. The event .load() method conflicted with the ajax .load() method. The .error() method could not be used with window.onerror because of the way the DOM method is defined. If you need to attach events by these names, use the .on() method, e.g. change $("img").load(fn) to $(img).on("load", fn).

Therefore, you need to change:

$(window).load(function() {}); 

to:

$(window).on("load", function (e) {}) 
like image 64
gaetanoM Avatar answered Sep 18 '22 19:09

gaetanoM