是否可以从父框架(相同的 document.domain)更改 iframe 的 DOM?
我想要一种简单且轻量级的方法来向我的 api 地址 (api.example.com) 发出跨域请求,该地址是我的主域 (example.com) 的子域em>)。
我已经阅读了很多有关处理 XDR 及其与每个使用的浏览器的不兼容性的技术和技巧,但 XDR 对我来说仍然非常复杂。我不需要像 easyXDM 这样完整/复杂的解决方案,我已经实现了它并且工作得很好。
因此,我决定“启用 CORS”,这解决了现代 Webkit 和 Gecko 浏览器的问题。但与往常一样,IE(在本例中也是 Opera)与 CORS 不兼容。
由于我想继续使用 jQuery AJAX 方法,因此我搜索了一个允许使用 jQuery 方法进行 XDR 的解决方案。然后,我通过 benvinegar 实现了一个非常好的解决方案,它取代了 jQuery.ajax() 方法及其所有依赖方法:https://gist.github.com/859940。他的脚本基于 document.domain/iframe 技巧。
在调用 Ben 提出的函数之前,我使用 jQuery.support.cors(使用 jQuery 1.6.2)测试了 CORS 支持。一切都很好,工作完美。
我对上面的链接脚本唯一不满意的是,我需要从 api.example.com 加载 jQuery 库,但我不希望这样。我在一个文件中制作了一系列缩小的 javascript 库/插件/脚本,供 example.com 使用;这给了我 2 个选项(在 api.example.com 中使用 jQuery):再次加载整个包或仅加载 jQuery 的非缓存版本。我也不喜欢。
我的问题是:当两个框架具有相同的 document.domain 时,是否可以从父框架更改 iframe 的 DOM?如果是这样,我可以将 jQuery 从 example.com 克隆到子 iframe (api.example.com) 中吗?如何?或者我只是对此感到疯狂,考虑到我所描述的内容,有更好的解决方案吗?
预先感谢大家, 莱昂纳多.
I want a simple and light-weighted way of making cross-domain requests to my api address (api.example.com) which is a subdomain of my main domain (example.com).
I've read A LOT about techniques and hacks to deal with XDRs and their incompatibilities with each used browser but XDR is still very complicated for me. I don't need a complete/complex solution as easyXDM, which I have implemented and worked perfectly.
So, I decided to 'enable CORS' which solved the problem for modern Webkit and Gecko browsers. But as always, there is the IE (and in this case Opera too) which is not compatible with CORS yet.
As I wanted to continue using jQuery AJAX methods, I searched for a solution that would allow XDR with jQuery methods. Then, I implemented a very nice solution by benvinegar which replaces jQuery.ajax() method and therefore all its dependent methods: https://gist.github.com/859940. His script is based on the document.domain/iframe trick.
Before calling the function proposed by Ben, I tested for CORS support with jQuery.support.cors (using jQuery 1.6.2). Everything is okay, working perfectly.
The only thing I'm not happy with the above linked script is that I need to load the jQuery library from api.example.com and I don't want that. I've made a bundle of minified javascript libraries/plugins/scripts in one file which is used by example.com; that gives me me 2 options (for using jQuery in api.example.com): load the entire bundle again or load a non-cached version of jQuery only. I don't like either.
My question is: is it possible to change the DOM of an iframe from the parent frame when both frames have the same document.domain? If so, could I clone jQuery from example.com into the child-iframe (api.example.com)? How? Or I'm just being crazy about this and there is a better solution taking in consideration what I described?
Thank you all in advance,
Leonardo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件 test.htm
文件 test2.htm
上面的代码将提醒“hi”,然后提醒“bye”
file test.htm
file test2.htm
The code above will alert 'hi' and then 'bye'