今天,我将Firefox升级到13.0。但是我的javascript代码出了问题。
有一个网页A(www.xx.com)和网页B(webim.xx.com)。我使用iframe标签将B嵌入到A中。
网页A
首先将域设置为“xx.com”
<script>document.domain = 'xx.com';</script>然后创建一个iframe来加载网页B。
<script>
var iframe = document.createElement('iframe');
document.body.insertBefore(iframe, document.body.firstChild)
iframe.src = 'http://webim.xx.com';
</script>网页B将域设置为“xx.com”
<script>document.domain = 'xx.com';</script>然后访问网页B的localStorage。
在网页A上,执行代码:
window.iframe.contentWindow.localStorage.setItem('a', 'a')然后会出现一个错误:
Error: The operation is insecure.在以前的版本或其他浏览器中,代码可以正常执行。
有人知道为什么吗?
是个窃听器?
然后..。如何解决这个问题?谢谢。
刚才我找到了解决这个问题的办法。
我不能直接访问localStorage,但是我可以调用-- iframe的函数,它可以调用自己网页的localStroage。
/// webpage B
<script>
document.domain = 'xx.com';
var ls = { ///< ls is short for localStorage.
setItem: function(k, v) {
return localStorage.setItem(k, v);
},
getItem: function(k) {
return localStorage.getItem(k);
},
removeItem: function(k) {
return localStorage.removeItem(k);
},
clear: function(){
return localStorage.clear();
}
}
</script>然后我调用、ls.setItem、等来访问iframe的localStorage。
/// webpage A
<script>iframe.ls.setItem('a', 'b');</script>即使我能解决这个问题,为什么firefox 13.0会导致这个问题呢?
发布于 2012-06-08 15:08:37
旧的Firefox行为是错误的,bug被修复了。按照规范,设置document.domain对localStorage的行为没有任何影响,所以在您的示例中,您试图为不同的域设置localStorage,这是不允许的。
有关详细信息,请参阅https://bugzilla.mozilla.org/show_bug.cgi?id=495337和localStorage规范。
https://stackoverflow.com/questions/10929337
复制相似问题