javascript window.location 在 Firefox 中不起作用

发布于 2024-11-26 19:14:15 字数 805 浏览 0 评论 0 原文

这确实让我头疼:

使用 javascript(假设在 page1.html 上),我使用类似于下面的代码来启动一个新窗口:

var popwindow = window.open("http://www.stackoverflow.com");

然后我将 popwindow 变量传递给使用 < code>setTimeout 每 3 或 4 秒重复一次(我认为这部分不太重要)。这个小循环代码的作用是(应该)检查弹出窗口的 location 对象,以查看 url 何时更改并包含某些特定的查询字符串变量。

在大多数情况下,这是有效的;代码重复,并且使用 firebug,我可以看到窗口对象正在按其应有的方式传递。

但我的问题是,每当我尝试访问 popwindow.location.hrefpopwindow.location.search 时,javascript 都会崩溃。我什至只是尝试自己做一个 popwindow.location.toString() 来获取这些东西,但这也会崩溃。

当我使用 firebug 时,我可以看到我尝试访问的 location 对象是合法的 location 实例;当我暂停脚本时,firebug 允许我浏览 location 对象的字段。然而,当我尝试访问这些东西时(我不是写信给它们,只是读取值),它永远不会工作......如果它有帮助,我正在使用最新版本的firefox(我相信是5.0)。

this one is really giving me a headache:

using javascript (let's say on page1.html) i use code similar to below to launch a new window:

var popwindow = window.open("http://www.stackoverflow.com");

i then pass the popwindow variable to a function that uses setTimeout to repeat every 3 or 4 seconds (this part is not too important i do not think). what this little looping code does is (supposed to) check the location object of the popup window to see when the url changes and contains some certain query string variable.

for the most part, this is working; the code repeats, and using firebug, i can see that the window object is getting passed as it should.

my issue though is that whenever i try to do any access to popwindow.location.href or popwindow.location.search, javascript crashes. i even simply tried to do a popwindow.location.toString() to get this stuff out myself, but that crashes as well.

when i use firebug, i can see that the location object that i am trying to access is a legit location instance; firebug allows me to browse the location object's fields when i have the scripts paused. however, when i try to access these things (i am not writing to them, only reading the values) it will never work... in case it is helpful, i am using the latest version of firefox (5.0 i believe).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一杯敬自由 2024-12-03 19:14:15

如果引用的窗口与您正在运行的脚本所在的窗口不在同一域中,则浏览器将不允许您访问跨域窗口。它会抛出异常。

If the referenced window is not on the same domain as the window that the script you're running is in, then the browser won't let you access cross domain windows. It will throw an exception.

平定天下 2024-12-03 19:14:15

来自 https://developer.mozilla.org/En/DOM/Window.open

窗口对象参考

这是指向新创建的浏览器窗口的引用。该引用是open()方法的返回值;如果由于某些原因执行未能成功打开窗口,则该值将为 null。最好使用全局变量来存储此类引用。例如,您可以使用它来查找新窗口的属性或访问其方法,假设您的主窗口与辅助窗口的关系符合 同源策略安全要求。

我怀疑这可能是你的问题。 “同源策略”链接更详细地记录了要求。

From https://developer.mozilla.org/En/DOM/Window.open

windowObjectReference

This is the reference pointing to the newly created browser window. This reference is the return value of the open() method; it will be null if for some reasons the execution did not succeed to open the window. A global variable is best used to store such reference. You can then, for example, use it to look for properties of the new window or access its methods, assuming that your main versus secondary window relationship complies with Same origin policy security requirements.

I suspect this is probably your problem. The "same origin policy" link documents the requirements in more detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文