Javascript document.open asynchronous?

Posted by Alex Schneider on Stack Overflow See other posts from Stack Overflow or by Alex Schneider
Published on 2012-12-10T22:37:24Z Indexed on 2012/12/10 23:04 UTC
Read the original article Hit count: 207

Filed under:
|
|

So on my site there is a Javascript function that will load a new site from the server via XMLHttpRequest. After that it replaces the current page with the new one:

var post = new XMLHttpRequest();
post.open('POST', data);
post.onload = function() {
    var do = document.open("text/html", "replace");
    do.write(post.responseText);
    do.close();
    goOn();
}

function goOn() {
    console.log($('img:visible'));
}

Some could assume that after do.close() the document has changed and is ready. But it is not, e.g. if i load very much/big data/responseText the function goOn() only logs an empty result. Obviously goOn() gets in that case called before the DOM is ready to be read! Unfortunately the is no "ready" event fired after write() finished.... How can i be sure it is finished?

/EDIT: goOn() logs this to Chrome Console:

[prevObject: p.fn.p.init[1], context: #document, selector: "img:visible"]
context: #document
length: 0
prevObject: p.fn.p.init[1]
selector: "img:visible"
__proto__: Object[0]

But if i right after that type $('img:visible') into console manually it shows me all images....

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about document