Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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