由网友(专一的男人)分享简介:我如何获得使用当前页的源代码的JavaScript 和 DOM ?我将不得不使用 AJAX ?解决方案 当前页面的HTML,所呈现:document.documentElement.outerHTML另外,你可以使用的innerHTML ,但你只能得到的含量<身体GT; 标记。的当前页面的HTML,作为检索的...![JavaScript制作文本打印输出效果](/d/file/2023/09-16/e16ad5d94bdc3932faac38faca7d3e40.png)
我如何获得使用当前页的源代码的JavaScript
和 DOM
?
我将不得不使用 AJAX
?
当前页面的HTML,所呈现:
document.documentElement.outerHTML
另外,你可以使用的innerHTML
,但你只能得到的含量<身体GT;
标记。的
当前页面的HTML,作为检索的页面加载:
动态与AJAX调用重新查询页面。
使用jQuery(坦途):
任何其他现代JavaScript库将允许做同样的事情,但为了举例,用jQuery这将是:
$。阿贾克斯(window.location.href,{
成功:功能(数据){
的console.log(数据);
}
});
使用XMLHtt prequest对象(更复杂的):
显然下去裸骨的路线,你需要自己处理一些事情,包括跨浏览器的支持,但它的要点是:
VAR请求=新XMLHtt prequest();
request.open(GET,window.location.href,假);
request.send();
如果(request.status === 200){
执行console.log(request.responseText);
}
![JavaScript制作文本打印输出效果](/d/file/2023/09-16/e16ad5d94bdc3932faac38faca7d3e40.png)
How do I obtain the current page's source using JavaScript
and DOM
?
Would I have to use AJAX
?
For the current page's HTML, as rendered:
document.documentElement.outerHTML
Alternatively, you could use innerHTML
, but you'd only get the content of the <body>
tag.
For the current page's HTML, as retrieved on page load:
Re-query the page dynamically with an AJAX call.
Using jQuery (easy path):
Any other modern JavaScript library would allow to do something similar, but for the sake of example, with jQuery it would be:
$.ajax(window.location.href, {
success: function (data) {
console.log(data);
}
});
Using the XMLHttpRequest object (more involved):
Obviously going down the bare-bone route, you'll need to handle a few more things yourself, including cross-browser support, but the gist of it would be:
var request = new XMLHttpRequest();
request.open('GET', window.location.href, false);
request.send();
if (request.status === 200) {
console.log(request.responseText);
}
相关推荐
最新文章