今天我將Firefox升級到13.0。但是我的JavaScript代碼出了問題。firefox 13.0跨域localStorage訪問:錯誤:操作不安全
有一個網頁A(www.xx.com)和網頁B(webim.xx.com)。我使用iframe標記在A中嵌入B.
網頁甲
首先設置域爲 '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>
網頁乙 將網域設爲'xx.com'
<script>document.domain = 'xx.com';</script>
然後我訪問網頁B.
的localStorage的在網頁上,執行代碼:那麼一個錯誤將給予
window.iframe.contentWindow.localStorage.setItem('a', 'a')
:
Error: The operation is insecure.
在前面版本或其他瀏覽器,代碼可以正常執行。
有人知道爲什麼嗎?
這是一個錯誤?
而且..如何解決這個問題?謝謝。
剛纔我找到了解決這個問題的方法。
我不能直接訪問本地存儲,但我可以叫的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會導致這個問題呢?
看來,如果我想清楚地理解它,我需要閱讀[這個規範](http://www.whatwg.org/specs/web-apps/current-work/multipage/webstorage。html#dom-localstorage)和[其他規範](http://www.whatwg.org/specs/web-apps/current-work/multipage/origin-0.html#origin)。 – Witcher42