【问题标题】:How to check if content exists in iframe with external source如何使用外部源检查内容是否存在于 iframe 中
【发布时间】:2013-11-28 13:54:10
【问题描述】:
<div class="cont">
    <iframe id="iframe" sandbox="" src="external_website" width="100%" height="100%" frameborder="0"></iframe>
</div>

我想用 JQuery 检测我的 iframe 中是否从其他网站获取内容?

我尝试了不同的方法,但没有任何帮助。 谢谢。

【问题讨论】:

  • 您无法访问来自不同域的 iframe 的内容。
  • 您应该能够检查 src 是本地的还是外部的。
  • 您正在对 iframe 进行沙盒处理,根本不允许脚本或任何跨源内容,那么为什么要将跨源 URL 加载到以这种方式进行沙盒处理的 iframe 中
  • 我已经改变了 - sandbox="allow-forms allow-scripts"。我想知道网站是否显示,否则不显示 iframe。

标签: jquery iframe external


【解决方案1】:
$("#myiframe").contents().find("#myContent")


<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">

$(function() {

    //here you have the control over the body of the iframe document
    var iBody = $("#iView").contents().find("body");

    //here you have the control over any element (#myContent)
    var myContent = iBody.find("#myContent");

});

</script>
</head>
<body>
  <iframe src="mifile.html" id="iView" style="width:200px;height:70px;border:dotted 1px red" frameborder="0"></iframe>
</body>
</html>

【讨论】:

  • 是的,但这不适用于其他域。并且必须使用属性沙箱。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-06
  • 1970-01-01
  • 2011-02-24
  • 2012-03-04
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
相关资源
最近更新 更多