Skip to content

Instantly share code, notes, and snippets.

@DaveChild
Created July 24, 2009 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveChild/154010 to your computer and use it in GitHub Desktop.
Save DaveChild/154010 to your computer and use it in GitHub Desktop.
JavaScript function that will fetch the source of the page being viewed. Based, I think, on http://www.astral-consultancy.co.uk/cgi-bin/hunbug/doco.cgi?11340
function viewSource() {
var httpRequest;
try {
httpRequest = new XMLHttpRequest();
}catch(trymicrosoft) {
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch(oldermicrosoft) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch(failed) {
httpRequest = false;
}
}
}
if(!httpRequest) {
return false;
}
httpRequest.onreadystatechange = function() {
if(httpRequest.readyState == 4) {
if(httpRequest.status == 200) {
document.body.innerHTML = '<pre id="pageSource"></pre>';
document.getElementById('pageSource').innerText = httpRequest.responseText;
}
}
}
httpRequest.open('GET',document.location.href,true);
httpRequest.send(null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment