0

I ran into an issue with the javascript property window.opener (which keeps a reference to the window that opened the popup) in conjunction with a redirect from within an applet. Whenever a redirect happens from within the applet, window.opener was changed to the current pop-up. Even stranger is the fact that this change was introduced with a recent java update quite recently: this did not happen with java 1.7.0_03-b05 but started happening with java 1.7.0_04-b20. Now this might be some exotic usecase but still, I'm wondering if this change was intentional.

To get to the bottom of this I have written a small test to demonstrate the behavior in question. The HTML-Sites can be found here. It does only work in Firefox (which is OK for my usecase)

  1. The index.html page contains the following 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;
    }
    

    I set the windows name to "index.html" (for identification purpose) and provide a function that will open applet.html in a popup-window. It also contains a link which calls this function:

    <a href="#" onclick="open_new_window(); return false;">open applet.html in new                
    window</a>
    
  2. after clicking the link, applet.html is opened in a popup. This contains the following 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';
    }
    

    I set this popup's window.name to "applet.html" (for identification purpose) and alert the current window.opener.name. When the body fires the onload event, I change the openers background color to red.

    Also, this page contains an applet, which has a button that will fire a redirect to 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. after clicking the button in the applet, redirect.html is opened in the same popup-window. This page contains the following 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';
    }
    

    I set this popup's window.name to "redirect.html" (for identification purpose) and alert the current window.opener.name. When the body fires the onload event, I change the openers background color to green.

With java 1.7.0_03-b5 the following happens:

  1. After clicking the link in index.html, applet.html is opened in a popup window. The alert shows index.html as being the openers name. Then the backgroundcolor of the opener is changed to red
  2. After clicking the button in the applet redirect.html is opened in the same window. The alert shows index.htmlas being the openers name. Then the backgroundcolor of the opener is changed to green

so far so good... but with java 1.7.0_04-b20 the following happens:

  1. After clicking the link in index.html, applet.html is opened in a popup window. The alert shows index.html as being the openers name. Then the backgroundcolor of the opener is changed to red
  2. After clicking the button in the applet redirect.html is opened in the same window. The alert shows 'redirect.html` as being the openersname. Then the backgroundcolor of the opener (which is the popup-window now!) is changed to green.

So it seems the redirect issued by the Applet via this.getAppletContext().showDocument(new URL(this.getDocumentBase(), "redirect.html"), "_self"); changes the window.opener property to the window it changed the URL in...

I would appreciate any insights into why this behavior might have changed or if you could imagine a workaround to this problem.

Thanks a lot for your time

3
  • Consider using a draggable applet instead. Note that most people will not especially care about yet another applet / JS / browser / JRE / focus - interaction bug or quirk. The only advice the top applet answerer can muster at this time is as follows. Stop pissing about with multiple browser windows. If you do, the pain is well deserved. Jul 31, 2012 at 0:21
  • Andrew, thanks for the comment and 'advice'. I wish i could 'stop pissing about with multiple browser windows' but as it is right now this decision is out of my hands. Jul 31, 2012 at 6:09
  • "right now this decision is out of my hands" Please assure me that you are charging 'top dollar' from whoever made the decision. :( Jul 31, 2012 at 7:49

1 Answer 1

1

Your post helped me understand that I'm not the only one going crazy here. I believe this is a bug in the new java version.

I simply added the following javascript to my popup page:

var parentWin = window.opener;

then, when I need to call the parent, I use parentWin. This solved my problem.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.