Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery equivalent of body onLoad

Is it allowed to use <body onLoad="myfunc()"> along with jQuery's document.ready() handlers? I can't find a way to achieve the same functionality of the <body onLoad> with jQuery.

An example of a use case would be a facebook application. An Iframe facebook app requires the use of the FB.Canvas.setSize function which resize the iframe.

I would need to fire it up only when all elements on the page are finished loading.

like image 681
Joel Avatar asked Jan 09 '11 08:01

Joel


People also ask

What is onload function in jQuery?

The onload event occurs when an object has been loaded. onload is most often used within the <body> element to execute a script once a web page has completely loaded all content (including images, script files, CSS files, etc.).

Is there any difference between body onload () and document ready () function?

The main differences between the two are: Body. Onload() event will be called only after the DOM and associated resources like images got loaded, but jQuery's document. ready() event will be called once the DOM is loaded i.e., it wont wait for the resources like images to get loaded.

When body onload function is called?

The body. onload() event will be called once the DOM and all associated resources like images got loaded. Basically, onload() will be called when the page has been fully loaded with entire images, iframes and stylesheets, etc.

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.


2 Answers

$(window).load(myfunc) is what you're looking for. If you're working with the load event of an iframe, you can replace window with the iframe element/selector.

like image 150
Kevin Avatar answered Oct 13 '22 01:10

Kevin


This works as well:

$(window).load(function(){     // ..     myfunc();     // .. }); 
like image 38
putude Avatar answered Oct 13 '22 00:10

putude