get full html source code of page through ajax req

2019-02-24 22:32发布

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();

1条回答
地球回转人心会变
2楼-- · 2019-02-24 23:37
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();
}

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);

then i host the js file on my computer.. i use analogx simpleserver... then you can use a full page for your script

查看更多
登录 后发表回答