Document.domain 屏蔽了 javascript 代码
Document.domain blocked javascript code
我已经实施了同源策略,我在其中使用了 iframe 和 javascripts document.domain 属性.
这是 iframe:
<iframe name="ss_iframe" id="ss_iframe" style="display:none" src="http://www.onedomain.com/sample/a"></iframe>
这个iframe的代码是:
<html>
<head>
<script type="text/javascript" src="pathto/js/jquery/jquery-1.4.2.js"></script>
<script type="text/javascript">
document.domain = 'example.com';
(function ($) {
$.shbar = {
init : function()
{
parent.$.shbar.init( $.shbar );
},
ajax : function(options){
$.ajax(options);
}
};
})(jQuery);
$(document).ready(function() {
$.shbar.init();
});
</script>
</head>
<body></body>
</html>
我有一个 javascript 文件正在加载此 iframe。现在我在网站上使用的 js 具有以下同源策略代码:
document.domain = 'example.com';
在这个 js 文件中,我实现了 html5 的本地存储,它工作正常。但是当我使用以下代码时:
window.addEventListener('storage', function(storageEvent){
var curId = window.localStorage.getItem('curId');
if( curId != '' && storageEvent.key == 'samplestorage'){
$('#msgCount_'+curId ).remove();
}
}, false);
此代码在 mozilla 中运行良好,但在 chrome 中不起作用。但是如果我从 js 文件中删除 document.domain 行,它在 chrome 中也能正常工作。
即使在 window 上使用的焦点事件如果有 document.domain 也不起作用,并且在评论 document.domain 后工作正常。
我不明白为什么 document.domain 会阻止该代码,实际上是因为 document.domain 还是其他原因。
感谢@zer00ne 和 techie_28 的回复和时间。
我找到了解决问题的方法。发布以防有人遇到同样的问题:
var storageEvent= $.Event("storage");
$(document).ready(function ()
{
$(window).bind('storage', onStorageEvent);
});
function onStorageEvent(storageEvent) {
//your code which should run in other tabs
};
window.onfocus = function(event){
event.stopPropagation();
$(window).trigger(storageEvent);
};
我已经实施了同源策略,我在其中使用了 iframe 和 javascripts document.domain 属性.
这是 iframe:
<iframe name="ss_iframe" id="ss_iframe" style="display:none" src="http://www.onedomain.com/sample/a"></iframe>
这个iframe的代码是:
<html>
<head>
<script type="text/javascript" src="pathto/js/jquery/jquery-1.4.2.js"></script>
<script type="text/javascript">
document.domain = 'example.com';
(function ($) {
$.shbar = {
init : function()
{
parent.$.shbar.init( $.shbar );
},
ajax : function(options){
$.ajax(options);
}
};
})(jQuery);
$(document).ready(function() {
$.shbar.init();
});
</script>
</head>
<body></body>
</html>
我有一个 javascript 文件正在加载此 iframe。现在我在网站上使用的 js 具有以下同源策略代码:
document.domain = 'example.com';
在这个 js 文件中,我实现了 html5 的本地存储,它工作正常。但是当我使用以下代码时:
window.addEventListener('storage', function(storageEvent){
var curId = window.localStorage.getItem('curId');
if( curId != '' && storageEvent.key == 'samplestorage'){
$('#msgCount_'+curId ).remove();
}
}, false);
此代码在 mozilla 中运行良好,但在 chrome 中不起作用。但是如果我从 js 文件中删除 document.domain 行,它在 chrome 中也能正常工作。
即使在 window 上使用的焦点事件如果有 document.domain 也不起作用,并且在评论 document.domain 后工作正常。
我不明白为什么 document.domain 会阻止该代码,实际上是因为 document.domain 还是其他原因。
感谢@zer00ne 和 techie_28 的回复和时间。
我找到了解决问题的方法。发布以防有人遇到同样的问题:
var storageEvent= $.Event("storage");
$(document).ready(function ()
{
$(window).bind('storage', onStorageEvent);
});
function onStorageEvent(storageEvent) {
//your code which should run in other tabs
};
window.onfocus = function(event){
event.stopPropagation();
$(window).trigger(storageEvent);
};