I have a MainContent div which contains main content of the website which could be loaded from ajax.
how can i find out if $("#MainContent").load("someUrl")
was called, so i am able to push new history state to the webbrowser?
Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to get or set the ID attribute value of an element. The following example will display the ID of the DIV element in an alert box on button click.
getElementById() to get the ID and store the ID into a variable. Then use JSON. stringify() method on the element (variable that store ID) and compare the element with 'null' string and then identify whether the element exists or not.
wrap( function ) A callback function returning the HTML content or jQuery object to wrap around the matched elements. Receives the index position of the element in the set as an argument. Within the function, this refers to the current element in the set.
length){ alert('Found with Length'); } if ($('#DivID'). length > 0 ) { alert('Found with Length bigger then Zero'); } if ($('#DivID') != null ) { alert('Found with Not Null'); } }); Which one of the 3 is the correct way to check if the div exists?
Like LSletty said, if you want to know when it is called use the handler from .load() itself:
$("#MainContent").load("path/content.html", function(){
// Do stuff when load is called ...
});
More info here: jQuery load event
To take action after completion I would use the .done() handler.
Use it in the following way:
$("#MainContent").load("path/content.html").done(function(){
// Do stuff when load is done ...
});
From jQuery docs:
Add handlers to be called when the Deferred object is resolved.
More info here: deferred.done() jQuery
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