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
$(window). bind("load", function() { // code here }); This works in all the case. This will trigger only when the entire page is loaded.
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.).
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.
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.
$(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 }); });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With