JavaScript获取当前页面的全部html代码

  使用JavaScript获取当前页面的所有html代码,包括文档申明,html根节点。

  第一种方法,使用ajax来加载页面,如下
+展开
-HTML
<script>
  var q=XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
  q.open("GET",location.href,false);
  q.send();
  document.write(q.responseText.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">"))
</script> 


  第二种方法,使用innerHTML,不过在IE核心的浏览器会把tag标签变为大写的,如果不在意大小写,可以使用如下代码。
+展开
-JavaScript
function encode(v){return v.replace(/</g,'<');}
window.onload=function(){
  var pre=document.createElement('pre');
  pre.innerHTML=typeof(HTMLElement) != "undefined"?
      encode(document.createElement("DIV").appendChild(document.documentElement.cloneNode(true)).parentNode.innerHTML):encode(document.documentElement.outerHTML);
  document.body.appendChild(pre);
}

加支付宝好友偷能量挖...


原创文章,转载请注明出处:JavaScript获取当前页面的全部html代码

评论(0)Web开发网
阅读(256)喜欢(0)JavaScript/Ajax开发技巧