30

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

James Hill
  • 60,353
  • 20
  • 145
  • 161
eozzy
  • 66,048
  • 104
  • 272
  • 428

8 Answers8

59
$(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
    });
});
BrTkCa
  • 4,703
  • 3
  • 24
  • 45
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
10

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.

James Hill
  • 60,353
  • 20
  • 145
  • 161
  • 1
    James has it right. Check out the documentation: http://api.jquery.com/ready/ ..the DOM will be loaded and ready for manipulation when this is called. – Craig Koster Jan 27 '12 at 16:52
  • 1
    And just as a quick note...I believe the difference between a window.load and a jQuery.ready is that window.load waits until *all* assets (e.g. stylesheets, images, etc) have been downloaded before it is called while jQuery.ready fires as soon as the DOM is loaded and doesn't care if images and such are still loading. – Craig Koster Jan 27 '12 at 16:55
  • 1
    "embed codes that insert HTML to the page dynamically" can very well be added to the DOM after the `ready` event is triggered. – Stefan Jan 27 '12 at 16:57
7

The load() method was deprecated in jQuery version 1.8 and removed in version 3.0. So you have to use -

$(window).on('load', function() {
 // code here
});
Atiq Baqi
  • 612
  • 1
  • 7
  • 16
7

Using the jQuery.ready should be enough. Try this

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

or

$(function(){

});

which is a shortcut of the first.

Juri
  • 32,424
  • 20
  • 102
  • 136
4

Try this:

$(document).ready(function () {
    if ( $('#abc')[0] ) { 
      alert("yes");
    }
});
DJ Quimby
  • 3,669
  • 25
  • 35
2

Generally, to handle my JQuery before or after page loads, will use:

jQuery(function($){
    // use jQuery code here with $ formatting
    // executes BEFORE page finishes loading
});

jQuery(document).ready(function($){
    // use jQuery code here with $ formatting
    // executes AFTER page finishes loading
});

ES6 Update

Here's the ES6 version of the script using the arrow function:

// Executes before page finishes loading.
document.addEventListener('DOMContentLoaded', () => {
  // Use JavaScript code here.
});

In the first example, we use the DOMContentLoaded event to execute the code when the HTML document has been completely loaded and parsed.

// Executes after page finishes loading.
window.addEventListener('load', () => {
  // Use JavaScript code here.
});

In the second example, we use the load event to execute the code after all page content has been loaded, including images and other external resources.

allenski
  • 1,652
  • 4
  • 23
  • 39
1
$(window).load(function () { ... }

can be enough but otherwise your embeded code (what ever that can be) might provide some callback functionality that you can make use of.

delay() should only be used to delay animations.

Stefan
  • 5,644
  • 4
  • 24
  • 31
0

Make sue you bind the event with dom load so it's there when trigger called. This is how you do it. Hope this helps someone someday

$(window).bind("load", function() { 
   //enter code here
   $("#dropdow-id").trigger('change');
});`