在所有浏览器中使用 window.opener 时出现的问题
好吧,我这里遇到了严重的问题。在我自己的计算机上,这段代码运行得很好。但在我的同事身上,它在 IE 和 Firefox 中崩溃了。
var openlocation = window.opener.location.href;
在 window.opener 部分因“权限被拒绝”而中断。因此,在开头页面上,我编写了一个存储在外部 .js 页面中的函数:
var getLocation = function() {
return window.location.href;
};
然后调用它,例如 var openlocation = window.opener.getLocation();
有谁知道为什么会出现这种情况吗?
编辑:一个页面正在创建弹出窗口,并且它们都位于同一域中。 弹出页面具有以下代码:
$(document).ready(function () {
var openlocation = window.opener.getLocation();
(function setTimer() {
setInterval(function () {
if (window.opener.location.href != openlocation) { // they've changed screens
window.close();
}
}, 15000);
})();
$("input#notescancel").click(function () {
window.close();
});
});
我尝试过使用 getLocation()
函数并仅使用 window.opener.location.href
但是,对于除我之外的所有计算机,它破裂了。 谢谢。
Okay, I'm having serious problems here. On my own computer, this code works just fine. But on my coworkers, it breaks in IE and Firefox.
var openlocation = window.opener.location.href;
breaks for "Permission Denied", on the window.opener part. So, on the opener page, I wrote a function that's stored in an external .js page:
var getLocation = function() {
return window.location.href;
};
and then calling that, likevar openlocation = window.opener.getLocation();
Does anyone have any idea why this is breaking?
Edit: One page is creating the popup, and they're both on the same domain.
The popup page has the following code:
$(document).ready(function () {
var openlocation = window.opener.getLocation();
(function setTimer() {
setInterval(function () {
if (window.opener.location.href != openlocation) { // they've changed screens
window.close();
}
}, 15000);
})();
$("input#notescancel").click(function () {
window.close();
});
});
I've tried with both my getLocation()
function and just using window.opener.location.href
but, for all computers but mine, it breaks.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是因为这两个窗口位于不同的域。即使是sample.com 和www.sample.com 也被浏览器认为是不同的。从技术上讲,它称为同源策略,
https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript
It may be because that the 2 windows are on different domains. Even sample.com and www.sample.com are considered different by the browser. Technically its called Same Origin Policy,
https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript
我想通了。我打算深入研究 window.opener.location - 对于 IE,我应该在那里停下来,将其转换为字符串,然后称其为好。这就是我所做的,现在它有效了。
因此,我只是在 IE 中使用 window.opener.location.toString(),而不是 window.opener.location.href。
I figured it out. I was going to deep into window.opener.location - for IE, I should stop it there, convert it to a string, and call it good. Which is what I did, and it now works.
So, instead of window.opener.location.href, I just used window.opener.location.toString() for IE.