JavaScript查看页面源

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript查看页面源相关的知识,希望对你有一定的参考价值。

  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. }

以上是关于JavaScript查看页面源的主要内容,如果未能解决你的问题,请参考以下文章