Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javascript访问IFrame的HTML_Javascript_Jquery_Html_Jsf_Iframe - Fatal编程技术网

使用Javascript访问IFrame的HTML

使用Javascript访问IFrame的HTML,javascript,jquery,html,jsf,iframe,Javascript,Jquery,Html,Jsf,Iframe,我正在尝试访问IFrame中显示的按钮。然后我想为该按钮附加一个侦听器。但是,我似乎无法访问IFrame。假设按钮的id为按钮。我怎么能这样做呢 以下是我的代码示例: <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.d

我正在尝试访问IFrame中显示的按钮。然后我想为该按钮附加一个侦听器。但是,我似乎无法访问IFrame。假设按钮的id为
按钮
。我怎么能这样做呢

以下是我的代码示例:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <script>
            function test() {
                var temp = $("myIframe").contents();
                alert(temp);
                var temp2 = temp.find("body");
                alert(temp2);
                var temp3 = temp2.html();
                alert(temp3);
            }
        </script>
        <h:form>
            <p:commandButton value="Click" onclick="test();"/>
        </h:form>
        <iframe id="myIframe" src="http://test.com" height="200" width="500"/>
    </h:body>
</html>

Facelet标题
功能测试(){
var temp=$(“myIframe”).contents();
警报(临时);
var temp2=临时查找(“主体”);
警报(temp2);
var temp3=temp2.html();
警惕(temp3);
}

temp和temp2警报正常,但temp3未定义。有人能帮忙吗?

出于安全原因,没有人能帮忙

想象一下,能够创建一个页面,在一个隐藏的IFRAME中打开Facebook,然后使用脚本窃取身份验证cookie或操纵DOM在每个人的墙上发布垃圾邮件


您无法访问其他网站上的内容有很好的理由。您必须实现另一种方式来访问这些信息,例如服务器端API。

您不能通过javascript访问iframe内容

这可能是一个严重的安全问题

您可以实现的唯一方法是将CURL与PHP/其他一些语言一起使用,并通过服务器呈现它

ajax(jquery)和php CURL示例

<script>
var lat = $.ajax({
url: "function.php?function=curl",
async: false
}).responseText;
</script>

如果您可以控制父帧和iframe,则可以通过子帧到父帧的消息传递来实现这一点

让父帧侦听消息

<div id="myDiv">
    <iframe src="childFrame.html" id="frame1"></iframe>
</div>

<script>    
window.onmessage = function(e) {
     if(e.data == "click") {
        alert("Button Clicked!");
     }
}
</script>

window.onmessage=函数(e){
如果(如数据=“点击”){
警报(“单击按钮!”);
}
}
iframe可以向父帧发送消息

<button onclick="parent.postMessage('click', '*');">Hide</button>
隐藏

IFRAME中的页面是否位于其他站点上?是的。这可能是任何网站的问题。我希望能够根据需要向按钮添加侦听器。您无法访问另一个域的IFRAME。没有IFRAME,有没有办法实现我想要的功能?也许使用标签?您可以使用jQuery检索html,然后将其插入dom。(嵌入与iframe具有相同的跨域限制)这非常有趣。我要去看看这个。我知道有这个图书馆。让我帮你找
<div id="myDiv">
    <iframe src="childFrame.html" id="frame1"></iframe>
</div>

<script>    
window.onmessage = function(e) {
     if(e.data == "click") {
        alert("Button Clicked!");
     }
}
</script>
<button onclick="parent.postMessage('click', '*');">Hide</button>