繁体   English   中英

使用javascript / jquery访问iframe的html

[英]Accessing the html of an iframe with javascript/jquery

我正在一个网站上工作,该网站要求用户登录在iframe中显示在我的网站上的第三方网站。 我正在尝试使用jQuery提取该网站的html代码,但实际上并没有用。 这是我的代码。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Iframe</title>
<link rel="stylesheet" type="text/css" href="style.css">

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">

$(document).ready(function(){
    console.log($("#iframe").contents().find("body"));
});

</script>
</head>

<body>
    <iframe src="https://www.google.com"></iframe>
</body>

</html>

我在示例中使用的是google,但我使用的是其他网站。 我得到的结果是-w.fn.init [prevObject:w.fn.init(0)]-但找不到html。 有谁知道该怎么做?

假设您的网站能够访问您所包含的任何iframe的内容。 您可以仅包括Facebook,Gmail或用户登录的任何银行网站之类的网站,然后窃取其个人信息。 因此,默认情况下严格禁止这样做: https : //developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

但是,有一种“选择加入”技术,可以在父窗口和iframe之间进行通信。 基本上,每个站点/文档都定义了要传输的信息,并使用window.postMessage()方法发送该信息: https : //developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

因此,如果消息是从一方发送的

window.postMessage("hello!", "http://example.com");

它可能会被其他人接收

window.addEventListener("message", function(e) {
    console.log(e.data); // prints "hello!"
}, false); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM