问题描述
javascript 代码将通过 google chrome 中的 url 栏从 www.example.com 启动,因此我无法使用 jquery.我的目标是在 www.example.com 中启动代码时,将 www.example.com/page.html 的完整 html 源代码传递给 javascript 中的一个变量.这可能吗?如果是这样怎么办?我知道要获取当前页面源,它只是 document.documentElement.outerHTML
,但我不确定如何执行此操作.我认为可以通过在以下代码中的某处使用 responseText
来实现:
The javascript code will be launched from www.example.com through the url bar in google chrome so i cannot make use of jquery. My goal is to pass the full html source code of www.example.com/page.html to a variable in javascript when i launch the code in www.example.com. Is this possible? If so how? I know to get the current page source it's just document.documentElement.outerHTML
but i'm not sure how i'd do this. I think it's possible by using responseText
somewhere in the following code:
http.send(params);
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","http://www.example.com/page.html",true);
xmlhttp.send();
推荐答案
data = ""
url = "http://www.example.com/page.html"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
data = xhr.responseText
}
}
xhr.send();
function process(){
url = "http://www.example.com/page.html"
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4){
alert(xhr.responseText)
}
}
xhr.send();
}
这就是我从地址栏运行脚本的方式..我一直这样做..我创建了一个这样的书签javascript:script=document.createElement('script');script.src='http://10.0.0.11/clear.js';document.getElementsByTagName('head')[0].appendChild(script);void(sss=1);
this is how i run script from the address bar.. I do it all the time..i create a bookmark like thisjavascript:script=document.createElement('script');script.src='http://10.0.0.11/clear.js';document.getElementsByTagName('head')[0].appendChild(script); void(sss=1);
然后我将 js 文件托管在我的计算机上..我使用analogx simpleserver...然后你可以为你的脚本使用一个完整的页面
then i host the js file on my computer.. i use analogx simpleserver... then you can use a full page for your script
这篇关于通过javascript通过ajax请求获取页面的完整html源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!