Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do jquery code AFTER page loading?

Tags:

jquery

If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

I want to do javascript code only after the page contents are loaded how can I do that?

like image 607
Alex Avatar asked May 28 '10 01:05

Alex


People also ask

How can we call a method after page load in jQuery?

If you want an event to work on your page, you should call it inside the $(document). ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

How do I call a jQuery page?

You can insert that code in your jQuery function IMO: $("foo"). click(function() { window. location="www.google.com"; });

What is the page load event in jQuery?

The load event occurs when a specified element has been loaded. This event works with elements associated with a URL (image, script, frame, iframe), and the window object. Depending on the browser, the load event may not trigger if the image is cached (Firefox and IE).


2 Answers

Use load instead of ready:

$(document).load(function () {  // code here }); 

Update You need to use .on() since jQuery 1.8. (http://api.jquery.com/on/)

$(window).on('load', function() {  // code here }); 

From this answer:

According to http://blog.jquery.com/2016/06/09/jquery-3-0-final-released/:

Removed deprecated event aliases

.load, .unload, and .error, deprecated since jQuery 1.8, are no more. Use .on() to register listeners.

https://github.com/jquery/jquery/issues/2286

like image 190
Matt Avatar answered Sep 28 '22 10:09

Matt


Following

$(document).ready(function() {  }); 

can be replaced

$(window).bind("load", function() {       // insert your code here  }); 

There is one more way which I'm using to increase the page load time.

$(document).ready(function() {    $(window).load(function() {       //insert all your ajax callback code here.       //Which will run only after page is fully loaded in background.   }); }); 
like image 23
Kshitiz Avatar answered Sep 28 '22 09:09

Kshitiz