Skip to content

Instantly share code, notes, and snippets.

@jeresig
Created September 27, 2010 17:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jeresig/599419 to your computer and use it in GitHub Desktop.
Save jeresig/599419 to your computer and use it in GitHub Desktop.
<?php header('HTTP/1.0 500 Internal Server Error'); exit; ?>
<!--
There is no way, in Opera, to figure out if an incoming page
is returning a 304 status.
Opera provides no indicator as to if a 304 occurred.
- xhr.status === 0
- xhr.statusText === ""
- xhr.getAllResponseHeader() === ""
- xhr.getResponseHeader( anything ) (not found)
All of the above is virtually identical to an aborted request in other browsers.
Example here: http://ejohn.org/files/bugs/304/
-->
<ul id="out"></ul>
<script>
window.onload = function() {
request("success.php", 200);
request("error.php", 500);
request("404.php", 404);
request("not_modified.php", 304);
request("wait.php", 0);
};
function request(url, expect) {
var xhr = window.XMLHttpRequest ? new window.XMLHttpRequest() : new window.ActiveXObject("Microsoft.XMLHTTP");
xhr.open( "GET", url, true );
var onreadystatechange = xhr.onreadystatechange = function() {
// If readyState === 0 then abort occurred, according to Opera
if ( xhr.readyState === 4 || xhr.readyState === 0 ) {
// Opera gives status of 0 for 500 errors
// Unfortunately this leads to ambiguity between errors and 304/Not Modified
// As not modified pages ALSO return a status of 0
if ( xhr.status === expect ) {
log( "PASS: " + url );
} else {
log( "FAIL: " + url + " (" + xhr.status + ")" );
}
xhr.onreadystatechange = null;
}
};
xhr.send( null );
if ( /wait/.test( url ) ) {
setTimeout(function(){
xhr.abort();
// Opera doesn't fire onreadystatechange on abort, fire it explicitly
if ( xhr.onreadystatechange ) {
onreadystatechange();
}
}, 13);
}
}
function log( msg ) {
document.getElementById("out").innerHTML += "<li>" + msg + "</li>";
}
</script>
<?php header('HTTP/1.0 304 Not Modified'); exit; ?>
<?php sleep(10); exit; ?>
@dossy
Copy link

dossy commented Sep 27, 2010

A quick Google search turns up this --

http://my.opera.com/community/forums/topic.dml?id=676132

Apparently, a known issue.

@tcaddy
Copy link

tcaddy commented Sep 27, 2010

I have gotten around a similar Opera bug by outputting a blank space (' ') as the body of my response. Example in PHP:

@fearphage
Copy link

There is a bug with 304 and empty bodies.

@wharrell1271
Copy link

http://www.w3.org/TR/XMLHttpRequest/#response "If the error flag is true return 0 and terminate these steps." that seems to be aggressively enforced on quite a few browsers. Makes it so that only 200s work. The "terminate these steps" is the real problem as the nest step is "Return the HTTP status code"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment