While playing with XHR requests I found interesting thing in making XHR requests from 2 major browsers - Google's Chrome and Mozilla's Firefox.
Request was simple HEAD
request to www.google.ba
:
var xhr = new XMLHttpRequest();
xhr.open('HEAD' , 'www.google.ba', true);
xhr.onreadystatechange = function (aEvt){
if (xhr.readyState == 4) {
if (xhr.status >= 200 && xhr.status < 304) {
console.log('Connected');
} else {
console.log('No net');
}
}
};
xhr.send();
After trying to make XHR request while there was no connection I would get readyState 4 and status 0 on both browsers. After i reconnected - making XHR request from both browsers were successful.
But here is the thing, after I disconnected again and made request, Chrome returned readyState 4 and status 0 after 9 milliseconds, but Firefox stayed in some kind of pending state and waited to net to return. When I reconnected after 13 seconds, request was successfully processed. I didn't get status 0 on Firefox even after 13 seconds. Is there anyone who could explained these huge differences and how to possibly prevent this behavior on Firefox?
Screenshot from Chrome:
Screenshot from Firefox:
EDIT:
It seems like that Firefox can't detect offline mode, only when I select work offline
in browser i get the same result as in Chrome. Is there any way to get same result as in Chrome?