I find myself doing this a lot:
window.onload = function(){
$.get("http://example.com/example.html", function(data) {
$('#contentHere').html(data);
setTimeout("javaScriptClass.init()", 200);
});
}
But setTimeout seems a bit hacky (and 200ms is already over three times the attention span of the average user :). What’s the best alternative?
EDIT
javaScriptClass.init() acts on DOM objects from what is loaded in the ajax call
I think there’s some confusion here about the load, you can just do this:
After the
$('#contentHere').html(data);the DOM elements will be there ready to use. Also take a look at.load()for attachment (in case otheronloadhandlers may need to attach), like this:Though, unless you’re waiting on images, this can be called in a
document.readyhandler and fire sooner, resulting in a better user experience.