Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from jeresig/error.php
Created September 27, 2010 17:26
Show Gist options
  • Save rwaldron/599431 to your computer and use it in GitHub Desktop.
Save rwaldron/599431 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; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment