JS获取整个页面的文档

这个是休息的时候想到了,不知道有没有人用过。就是在当前页面用AJAX请求当前页面,返回的就是整个页面的HTML··既然是整个页面,所以也包括文档声明。唯一需要注意的地方:innerText与textContent,显示页面的时候不能用innerHTML,否则会被解析。innerText与textContent是在除FF之外的浏览器与FF之间的差异。var innerText = document.body.innerText ? 'innerText' : 'textContent';上面的语句在开头处理以避免多次判断demo:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <div id="test"> <pre> 休息休息 </pre> </div> <script type="text/javascript" src="ajax.js"></script> <script type="text/javascript"> var connector = (function(){ var connector = null; if(window.XMLHttpRequest){ connector = new XMLHttpRequest(); }else if(window.ActiveXObject){ connector = new ActiveXObject('Microsoft.XMLHTTP'); } return connector; })(); var innerText = document.body.innerText ? 'innerText' : 'textContent'; var handler = function(response){ document.getElementById('test').getElementsByTagName('pre')[0][innerText] = response; } connector.onreadystatechange = (function(callback){ return function(){ if(connector.readyState == 4){//这里connector.status == 200都省了。 callback.call(connector,connector.responseText); } } })(handler); connector.open('GET',location.href,true);//发送到本页面 connector.send(); </script> </body> </html>

本文出自:PHP开发者联盟 http://www.hbcsdn.tk

原文链接:http://www.hbcsdn.tk/forum.php?mod=viewthread&tid=64&extra=page%3D1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值