跨域哈希更改通信 [英] Cross-domain hash change communication

查看:114
本文介绍了跨域哈希更改通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下两个网域:domain1.com和domain2。

Please consider the following two domains: domain1.com and domain2.

从domain1开启指向domain2的iframe。

From domain1 I open an iframe that points to domain2.

现在,我想让这些家伙彼此沟通,我已经成功地通过在两个域上应用哈希更改事件侦听器。

Now, I want these guys to communicate with each other, which I've successfully accomplished by applying hash change event listeners on both domains.

这样,父窗口(domain1)中的哈希将触发如果domain2使用新的哈希调用parent.location。此外,如果我从父级将其src属性更改为新的哈希,则在iframe中触发哈希更改事件。

That way, the hash in the parent window (domain1) will trigger if domain2 calls parent.location with a new hash. Also, the hash change event triggers in the iframe if I from the parent changes its src attribute to a new hash.

这很好!

这里有麻烦:

浏览器中的前进和后退功能被打乱了。简单来说,通过创建两个哈希实例,浏览器后退按钮必须点击两次才能更改父哈希,因为它必须先遍历iframe的哈希。

The back and forward functionality in the browser gets messed up. Simply put, by creating two hash instances, the browser back button has to be clicked twice to get the parent hash to change since it has to cycle through the iframe's hash first.

谢谢!

推荐答案

使用 easyXDM ,这是一个javascript库,为您做所有的辛勤工作,让您在所有浏览器,包括IE6中进行跨域通信和RPC。

Use easyXDM, it's a javascript library that does all the hard work for you, enabling you to do cross-domain communication and RPC in all browsers, including IE6.

这不会使用任何当前浏览器的HashTransport(甚至IE6)不改变历史。

This will not use the HashTransport for any of the current browsers (not even IE6), and so will not change the history.

你不会找到更好的东西。

You will not find anything better..

Script Junkie文章中的内部工作原理,或直接进入< a href =https://github.com/oyvindkinsey/easyXDM/#readme> readme at github

You can read about some of its inner workings in this Script Junkie article, or go straight to the readme at github

这篇关于跨域哈希更改通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆