通过javascript通过ajax请求获取页面的完整html源代码 [英] get full html source code of page through ajax request through javascript

查看:47
本文介绍了通过javascript通过ajax请求获取页面的完整html源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 this javascript: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源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆