使用给定的函数发布消息,但出现错误“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
使用 structured clone algorithm 发送消息在 Firefox 中,因此在发送之前需要调整某些内容。
在您的示例中,消息包含的内容并不明显,但绕过结构化克隆的一种 hack-y 方法是强制执行。通过 postMessage
发送 URL 会抛出错误:
someWindow.postMessage(window.location, '*');
// ERROR
但是您可以这样做来解决它:
var windowLocation = '' + window.location;
someWindow.postMessage(windowLocation, '*');
// WORKS
有更好的方法来处理这个问题,但对于您提供的内容,这至少应该允许一致的行为。
关于javascript - FireFox 34 中的 "DataCloneError: The object could not be cloned.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27558398/