JavaScript View Page Source


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function viewSource() {
  2. var httpRequest;
  3. try {
  4. httpRequest = new XMLHttpRequest();
  5. }catch(trymicrosoft) {
  6. try {
  7. httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  8. } catch(oldermicrosoft) {
  9. try {
  10. httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  11. } catch(failed) {
  12. httpRequest = false;
  13. }
  14. }
  15. }
  16. if(!httpRequest) {
  17. return false;
  18. }
  19.  
  20. httpRequest.onreadystatechange = function() {
  21. if(httpRequest.readyState == 4) {
  22. if(httpRequest.status == 200) {
  23. document.body.innerHTML = '<pre id="pageSource"></pre>';
  24. document.getElementById('pageSource').innerText = httpRequest.responseText;
  25. }
  26. }
  27. }
  28. httpRequest.open('GET',document.location.href,true);
  29. httpRequest.send(null);
  30. }

URL: http://www.addedbytes.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.