(This question is similar to this one, but it's for using XMLHttpRequest instead of an iframe for Comet.)
I'm starting an async long poll like this:
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.send();
If I do this inside <script>...</script>
in the head, it will cause the document to keep loading forever. (I'm testing this in Safari on Mac OS X and the iPhone, and it's the only browser I need to support).
Using DOMContentLoaded
or load
events won't work.
Using a setTimeout with a large enough delay will work. 0 won't, 1000 will, 100 will some times and not other times. I don't feel comfortable with this.
The only way I found that works is the combination of both:
document.addEventListener('DOMContentLoaded', function () {
setTimeout(function () {
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.send();
}, 0);
});
I guess this solves the problem for now, but I'm still afraid it will break in the future. // Edit: this doesn't work reliably either.
Does anyone know of a more reliable way?