在浏览器窗口中显示文本

时间:2014-05-18 17:34:41

标签: javascript jquery html cross-domain

我希望使用以下代码

在我打开的窗口中显示文本
  var yy = window.open("http://www.vignanuniversity.org/");

现在我希望在该窗口中显示我使用的文本

            var responseText = yy.html();

我在Chrome控制台中收到错误

      Protocols, domains, and ports must match.

所以我使用跨域,然后如何解决我的问题。

1 个答案:

答案 0 :(得分:0)

无法访问其他网页的窗口内容。但是,您可以让javascript在后端向您的服务器发出请求,以便为您提出请求。

<强>的Javascript

//Gets the contents of the web page using the backend server
getContents("http://www.vignanuniversity.org/", function(contents, responseCode){
    alert("Page received with status: " responseCode);
    alert(contents);
});

//Takes the URL as page and a function to handle the result.
//retFunc has one parameter, which is the response
function getContents(page, retFunc){
    var myServer = "/php/getRemote.php";
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4){
            retFunc(xmlhttp.responseText, xmlhttp.status);
        }
    }
    xmlhttp.open("GET", myServer + "?url=" + page);
    xmlhttp.send();
}

<强> PHP

//Location: /php/getRemote.php
$url = $_GET["url"];
echo file_get_contents($url);

这将使您有效地获取网站的内容。您刚刚无法访问修改后的内容