简体   繁体   English

如果来自同一域的2个脚本跨域?

[英]Cross-domain if 2 scripts from same domain?

I have a JS file that puts an iframe on every site its on. 我有一个JS文件,可将iframe放在每个网站上。
both the JS and the iframe location comes from the same domain, mine. JS和iframe位置都来自同一个域,我的域。

Can I somehow communicate from within the iframe to the outside script, 我可以通过某种方式从iframe内部与外部脚本进行交流,
which isn't running on my domain, but is called from it? 哪个不在我的域上运行,而是从中调用的?

I know about JSONP but i'm looking for a better way if possible 我了解JSONP,但我正在寻找更好的方法

It's very simple. 非常简单 The location of the document needs to be served from the same domain as the location of the script. 文档的位置需要与脚本的位置位于同一域中。 Otherwise, the script will get "Access Denied" error. 否则,脚本将出现“访问被拒绝”错误。

If you are supplying a script for other people to use, then you could have them load easyXDM which would allow your script to communicate with the document loaded in the iframe (if it also has the easyXDM library set up). 如果要提供脚本供其他人使用,则可以让他们加载easyXDM ,这将允许您的脚本与iframe中加载的文档进行通信(如果还设置了easyXDM库)。 You would also have to require them to host a simple html file on their domain in case easyXDM cannot use postMessage and has to resort to using the hash/fragment solution. 您还必须要求他们在其域上托管一个简单的html文件,以防easyXDM无法使用postMessage并不得不使用哈希/碎片解决方案。

Demos of this can be viewed here 演示可以在这里查看

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

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