我遇到了 javascript 属性 window.opener
的问题(它保留对打开弹出窗口的窗口的引用)与小程序内的重定向结合使用。每当小程序内发生重定向时,window.opener
已更改为当前的弹出窗口。更奇怪的是,这个更改是最近通过最近的 java 更新引入的:java 1.7.0_03-b05
并没有发生这种情况。但开始发生 java 1.7.0_04-b20
。现在这可能是一些奇特的用例,但我仍然想知道这种变化是否是故意的。
为了弄清楚这一点,我编写了一个小测试来演示相关行为。 HTML 站点可以找到 here 。它仅适用于 Firefox(这对我的用例来说没问题)
index.html
页面包含以下 JavaScript:window.name = "index.html"; function open_new_window() { window.open('applet.html', 'applet.html', 'width=1050,height=680,scrollbars=yes,status=yes').focus(); return false; }
我将窗口名称设置为“index.html”(用于识别目的),并提供一个在弹出窗口中打开 applet.html 的函数。 它还包含一个调用此函数的链接:
<a href="#" onclick="open_new_window(); return false;">open applet.html in new window</a>
点击链接后,
applet.html
在弹出窗口中打开。其中包含以下 JavaScript:window.name = "HelloWorldApplet.html"; alert("opener is now: "+window.opener.name); function on_load() { alert("changing bgcolor of opener to red"); window.opener.document.body.style.background='red'; }
我将此弹出窗口的 window.name 设置为“applet.html”(用于识别目的)并提醒当前
window.opener.name
。当主体触发 onload 事件时,我将开启器背景颜色更改为红色。此外,此页面包含一个小程序,其中有一个按钮可以触发到
redirect.html
的重定向。import java.applet.Applet; import java.awt.Button; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; public class HelloWorldApplet extends Applet implements ActionListener { Button redirectButton; @Override public void init() { super.init(); setLayout(new FlowLayout()); redirectButton = new Button("Redirect now!"); add(redirectButton); redirectButton.addActionListener(this); } @Override public void actionPerformed(ActionEvent ae) { try { this.getAppletContext().showDocument(new URL(this.getDocumentBase(), "redirect.html"), "_self"); } catch (MalformedURLException ex) { Logger.getLogger(HelloWorldApplet.class.getName()).log(Level.SEVERE, null, ex); } } }
单击小程序中的按钮后,
redirect.html
在同一个弹出窗口中打开。该页面包含以下 JavaScript:window.name = "redirect.html"; alert("opener is now: "+window.opener.name); function on_load() { alert("changing bgcolor of opener to green"); window.opener.document.body.style.background='green'; }
我将此弹出窗口的 window.name 设置为“redirect.html”(用于识别目的)并提醒当前
window.opener.name
。当主体触发 onload 事件时,我将开启器背景颜色更改为绿色。
与 java 1.7.0_03-b5
发生以下情况:
- 点击
index.html
中的链接后,applet.html
在弹出窗口中打开。警报显示index.html
作为开场白。然后打开器的背景颜色更改为红色 - 点击小程序中的按钮后
redirect.html
在同一窗口中打开。警报显示index.html
作为开场白。然后打开器的背景颜色更改为绿色
到目前为止一切顺利...但是 java 1.7.0_04-b20
发生以下情况:
- 点击
index.html
中的链接后,applet.html
在弹出窗口中打开。警报显示index.html
作为开场白。然后打开器的背景颜色更改为红色 - 点击小程序中的按钮后
redirect.html
在同一窗口中打开。该警报将“redirect.html”显示为开启者名称。然后打开器(现在是弹出窗口!)的背景颜色更改为绿色。
看来Applet通过 this.getAppletContext().showDocument(new URL(this.getDocumentBase(), "redirect.html"), "_self");
发出了重定向将 window.opener 属性更改为它更改了 URL 的窗口...
如果您能了解此行为可能发生变化的原因,或者您是否能想象出解决此问题的方法,我将不胜感激。
非常感谢您的宝贵时间
最佳答案
你的帖子让我明白,我并不是唯一一个在这里发疯的人。我相信这是新的java版本中的一个错误。
我只是将以下 JavaScript 添加到我的弹出页面中:
varparentWin = window.opener;
然后,当我需要调用父级时,我使用parentWin。这解决了我的问题。
关于java - 在 Java 1.7.0_04-b20 中从 Applet 重定向期间 window.opener 引用发生更改,但在 1.7.0_03-b05 中则没有更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11723635/