简体   繁体   English

跨域散列更改通信

[英]Cross-domain hash change communication

Please consider the following two domains: domain1.com and domain2. 请考虑以下两个域:domain1.com和domain2。

From domain1 I open an iframe that points to domain2. 从domain1我打开一个指向domain2的iframe。

Now, I want these guys to communicate with each other, which I've successfully accomplished by applying hash change event listeners on both domains. 现在,我希望这些人能够相互通信,我已经通过在两个域上应用哈希更改事件侦听器成功完成了。

That way, the hash in the parent window (domain1) will trigger if domain2 calls parent.location with a new hash. 这样,如果domain2使用新哈希调用parent.location,则将触发父窗口(domain1)中的哈希。 Also, the hash change event triggers in the iframe if I from the parent changes its src attribute to a new hash. 此外,如果来自父级的I将其src属性更改为新哈希,则哈希更改事件将在iframe中触发。

This works great! 这很棒!

Here comes the trouble: 麻烦来了:

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. 简单地说,通过创建两个哈希实例,必须单击浏览器后退按钮两次才能更改父哈希,因为它必须首先循环遍历iframe的哈希。

How can I communicate with a cross-domain iframe 2-way without screwing up the history object? 如何在不搞砸历史对象的情况下与跨域iframe 2路进行通信?

Thanks! 谢谢!

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. 使用easyXDM ,它是一个为您完成所有艰苦工作的JavaScript库,使您能够在所有浏览器(包括IE6)中进行跨域通信和RPC。

This will not use the HashTransport for any of the current browsers (not even IE6), and so will not change the history. 这不会将HashTransport用于任何当前浏览器(甚至不是IE6),因此不会更改历史记录。

You will not find anything better.. 你找不到更好的东西..

You can read about some of its inner workings in this Script Junkie article , or go straight to the readme at github 你可以在这篇Script Junkie文章中阅读它的一些内部工作原理,或直接阅读github上的自述文件

Another technique for crossdomain communications is (ab)using window.name . 另一种跨域通信技术是(ab)使用window.name It requires an iframe to originally have a same-domain src initially after which you move to another domain that sets the window.name and then steps back to the original source (step back in history). 它需要iframe最初最初具有同域src,之后您将移动到设置window.name的另一个域,然后返回原始源(历史记录中的后退)。 The idea is that the window.name does not change unless it's explicitly set, this means you can transfer window.name data cross domain. 我们的想法是,除非明确设置,否则window.name不会更改,这意味着您可以传输window.name数据跨域。

This technique is described in more detail on: 该技术在以下方面有更详细的描述:
- http://skysanders.net/subtext/archive/2010/10/11/leveraging-window.name-transport-for-secure-and-efficient-cross-domain-communications.aspx - http://skysanders.net/subtext/archive/2010/10/11/leveraging-window.name-transport-for-secure-and-efficient-cross-domain-communications.aspx
- http://jectbd.com/?p=611 - http://jectbd.com/?p=611

Be sure to choose the implementation that avoids clicking sounds in IE. 请务必选择避免在IE中单击声音的实现。

Unfortunatly, it still messes around with your history, but it does a step forward and then backwards to the history point it was at. 不幸的是,它仍然与你的历史息息相关,但它向前迈出了一步,然后又回到了它所处的历史点。 A big benefit though, is that you don't have to parse and encode URI strings, but can use JSON right away. 但是,一个很大的好处是,您不必解析和编码URI字符串,但可以立即使用JSON。

Using JSON lib for example 以JSON lib为例

// access window.name from parent frame
// note: only when iframe stepped back to same domain.
var data = JSON.parse( iframe.contentWindow.name );

// set child frame name
// note: only when iframe stepped back to same domain.
iframe.contentWindow.name = JSON.stringify( {
    foo : "bar"
} ); // to JSON string

// set own name ( child frame )
window.name = JSON.stringify( {
    foo : "bar"
} ); // to JSON string

The cookie technique is viable as well, for both techniques you need to perform ajax requests in the target iframe if you want to avoid history changes but still require http request. cookie技术也是可行的,如果您想要避免历史记录更改但仍需要http请求,则需要在目标iframe中执行ajax请求的两种技术。 so: 所以:

  1. Send data to iframe x (using cookie or window.name technique) 将数据发送到iframe x(使用cookie或window.name技术)
  2. Catch data with poller in iframe x 使用iframe x中的poller捕获数据
  3. Perform ajax requests in iframe x. 在iframe x中执行ajax请求。
  4. Send data back to iframe y (using cookie or window.name technique) 将数据发送回iframe y(使用cookie或window.name技术)
  5. Catch data with poller in iframe y 使用iframe y中的poller捕获数据
  6. Do the hokey pokey. 做笨蛋。

Any page refresh (httprequest) or url change will update the history (except for old or all IE versions), so more code is required alas. 任何页面刷新(httprequest)或URL更改都将更新历史记录(旧版本或所有IE版本除外),因此需要更多代码。

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

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