使用给定的函数来发布消息,但在 "target['postMessage'](message, target_url.replace([^:]+:/[^]+.*, '$1') "行得到错误 "DataCloneError: target['postMessage'](message, target_url.replace( ([^:]+:/[^]+).*, '$1'));",在FireFox-34中,同样的代码在Chrome和旧版本的FireFox上工作正常。
var storage = function() {
return {
postMessage : function(message, target_url, target) {
if (!target_url) {
return;
}
var target = target || parent; // default to parent
if (target['postMessage']) {
// the browser supports window.postMessage, so call it with a targetOrigin
// set appropriately, based on the target_url parameter.
target['postMessage'](message, target_url.replace( /([^:]+:\/\/[^\/]+).*/, '$1'));
}
}
}
}();
postMessage
发送消息,使用 结构化克隆算法 在Firefox中,正因为如此,你需要在发送之前调整一些东西。
在你的例子中,它并不明显地包含什么信息,但有一种黑客方法可以绕过结构化克隆,那就是胁迫一下。通过以下方式发送一个URL postMessage
会抛出一个错误。
someWindow.postMessage(window.location, '*');
// ERROR
但你可以这样做来解决它。
var windowLocation = '' + window.location;
someWindow.postMessage(windowLocation, '*');
// WORKS
有更好的方法来处理这个问题 但就你提供的东西而言,这至少应该允许一致的行为。