window.opener引用在Java 1.7.0_04-b20中从Applet重定向期间发生了更改,但在1.7.0_

window.opener reference is changed during redirect from Applet in Java 1.7.0_04-b20 but not in 1.7.0_03-b05

本文关键字:发生了 但在 重定向 04-b20 引用 opener Java 中从 window Applet      更新时间:2024-04-26

我遇到了javascript属性window.opener(它保留了对打开弹出窗口的窗口的引用)与小程序内重定向的问题。每当小程序中发生重定向时,window.opener都会更改为当前弹出窗口。更奇怪的是,这一变化是在最近的java更新中引入的:java 1.7.0_03-b05没有发生这种情况,但java 1.7.0_04-b20开始发生这种情况。现在,这可能是一些奇特的用例,但我仍然想知道这种变化是否是故意的。

为了弄清真相,我写了一个小测试来演示有问题的行为。HTML网站可以在这里找到。它只在Firefox中工作(这对我的用例来说是可以的)

  1. 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"(用于识别目的),并提供了一个在弹出窗口中打开apple.html的功能。它还包含一个调用此函数的链接:

    <a href="#" onclick="open_new_window(); return false;">open applet.html in new                
    window</a>
    
  2. 单击链接后,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);
            }
        }
    }
    
  3. 单击小程序中的按钮后,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,会发生以下情况:

  1. 单击index.html中的链接后,会在弹出窗口中打开applet.html。该警报显示index.html为开场白名称。然后,开场白的背景颜色变为红色
  2. 单击小程序中的按钮后,redirect.html将在同一窗口中打开。该警报显示index.html为开场白名称。然后,开场白的背景颜色变为绿色

到目前为止还不错。。。但对于java 1.7.0_04-b20,会发生以下情况:

  1. 单击index.html中的链接后,将在弹出窗口中打开applet.html。该警报显示index.html为开场白名称。然后,开场白的背景颜色变为红色
  2. 单击小程序中的按钮后,redirect.html将在同一窗口中打开。警报显示"redirect.html"为打开程序名称。然后,开场白的背景颜色(现在是弹出窗口!)变为绿色

因此,似乎Applet通过this.getAppletContext().showDocument(new URL(this.getDocumentBase(), "redirect.html"), "_self");发出的重定向将window.opener属性更改为它在…中更改URL的窗口

如果能深入了解这种行为可能发生变化的原因,或者你是否能想象出解决这个问题的方法,我将不胜感激。

非常感谢您抽出时间

你的帖子让我明白,我不是唯一一个在这里发疯的人。我相信这是新java版本中的一个错误。

我只是在弹出页面中添加了以下javascript:

var parentWin=window.opener;

然后,当我需要呼叫家长时,我使用parentWin。这解决了我的问题。