Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do something AFTER the page has loaded completely

Tags:

I'm using some embed codes that insert HTML to the page dynamically and since I have to modify that dynamically inserted HTML, I want a jquery function to wait until the page has loaded, I tried delay but it doesnt seem to work.

So for example, the dynamically inserted HTMl has an element div#abc

and I have this jquery:

if ( $('#abc')[0] ) {    alert("yes"); } 

the alert doesn't show up.

I'd appreciate any help

Thanks

like image 292
eozzy Avatar asked Jan 27 '12 16:01

eozzy


People also ask

How do I run a function after the page is loaded?

$(window). bind("load", function() { // code here }); This works in all the case. This will trigger only when the entire page is loaded.

What event do you use to perform something after the page has finished loading?

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.).

How do you check if page is loaded completely?

You can check the document. readyState property. From MDN: Returns "loading" while the document is loading, "interactive" once it is finished parsing but still loading sub-resources, and "complete" once it has loaded.

When Page is fully loaded JS?

In pure JavaScript, the standard method to detect a fully loaded page is using the onload event handler property. The load event indicates that all assets on the webpage have been loaded. This can be called with the window. onload in JavaScript.


2 Answers

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

If you have to wait for an iframe (and don't care about the assets, just the DOM) - try this:

$(document).ready(function() {      $('iframe').load(function() {         // do something     }); }); 
like image 59
Zoltan Toth Avatar answered Oct 04 '22 05:10

Zoltan Toth


That is the purpose of jQuery's .ready() event:

$(document).ready(function() {     if ( $('#abc').length ) //If checking if the element exists, use .length         alert("yes"); }); 

Description: Specify a function to execute when the DOM is fully loaded.

like image 36
James Hill Avatar answered Oct 04 '22 07:10

James Hill