繁体   English   中英

在跨域iframe内容的头部插入js

[英]Insert a js on head of cross-domain iframe content

我的问题是:我有一些带有下拉菜单的标签,每个标签都有一个iframe: 我的标签

当用户单击下拉菜单时,我需要在iframe内容上运行一些js,然后我认为正确的做法是在html页面中使用此js:

    var scriptTag = "<script>alert("blabla");<"; // saving script to run
    scriptTag += "/script>"; //add this for fixing the script tag problem
    $("#myframeid").contents().find("head").append(scriptTag); //appending the script on the head of iframe

但是当我运行它时,控制台报告如下:

未捕获的SecurityError:无法从“ HTMLIFrameElement”读取“ contentDocument”属性:阻止了源为“ http:// localhost:8080 ”的框架访问源为“ http://www.otherdomainsite.com ”的框架。 协议,域和端口必须匹配。

显然,如果它在其他域中无法运行,那么我现在该怎么办? 难道我做错了什么?

我听说过postMessage ecc ..方法,但从未使用过。

先感谢您。

不允许您针对外部域IFRAME执行代码。

唯一的方法是为每个消息传递方向提供代理iFrame。

三元实验室

暂无
暂无

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

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