Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4684153

LiveConnect method eval() runs in incorrect context when using frames (IE only)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.2_07
    • 1.3.1_20, 1.4.0, 1.4.2
    • deploy
    • b01
    • x86, sparc
    • solaris_2.5.1, windows_2000, windows_xp

        Name: gm110360 Date: 05/13/2002


        FULL PRODUCT VERSION :
        java version "1.4.0"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
        Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

        FULL OPERATING SYSTEM VERSION :
        Microsoft Windows 2000 [Version 5.00.2195]


        ADDITIONAL OPERATING SYSTEMS :
        This bug occours in Internet Explorer 5.x and 6, but not in
        Netscape 6/Mozilla.

        A DESCRIPTION OF THE PROBLEM :
        When using an applet in a frameset, LiveConnect
        JSObject.eval() in other
        frames than the one containing the applet fails. The script
        code is evaluated in the context of the window containing
        the applet instead of the one specified.


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        See the following example from the test case below. We have
        a frameset of two frames,
        the one to the left containing an applet:

        |---------------|
        |Frame1 |Frame2 |
        | | |
        |Applet | |
        |---------------|

        The applet does the following:

        JSObject win = JSObject.getWindow(applet);
        JSObject parent = (JSObject)win.getMember("parent");
        JSObject frame2 = (JSObject)parent.getMember("Frame2");
        frame2.eval("document.body.style.backgroundColor='green'");


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        This is supposed to turn the background to green in Frame2,
        but instead Frame1 turns green. If Frame2 contains a
        JavaScript method called foo() and you try to do
        frame2.eval("foo()") that does not work either, because it
        tries to evaluate foo() in Frame1.


        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        Place all three files in the same directory.

        testcase.html:
        BEGIN HTML SOURCE
        -------------------
        <html>
        <frameset cols="50%,50%">
          <frame name="Frame1" src="frame1.html">
          <frame name="Frame2" src="about:blank">
        </frameset>
        </html>
        -------------------
        END HTML SOURCE

        frame1.html
        BEGIN HTML SOURCE
        -------------------
        <html>
          <body>
            <applet code="Test.class" width=100 height=50 MAYSCRIPT></applet>
          </body>
        </html>
        -------------------
        END HTML SOURCE

        Test.java
        BEGIN JAVA SOURCE
        -------------------
        import java.applet.Applet;
        import java.awt.*;
        import java.awt.event.*
        import netscape.javascript.*;

        public class Test extends Applet implements ActionListener {
            Button button;

            public void init() {
                button = new Button("Run testcase");
                button.addActionListener(this);
                add(button);
            }

            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == button) {
                    try {
                        JSObject win = JSObject.getWindow(this);
                        JSObject parent = (JSObject)win.getMember("parent");
                        JSObject frame2 = (JSObject)parent.getMember("Frame2");
                        frame2.eval("document.body.style.backgroundColor='green'");
                    } catch(Exception ex) { System.err.println("Exception
        thrown:"+ex); }
                }
            }
        }
        -------------------
        END JAVA SOURCE

        ---------- END SOURCE ----------

        CUSTOMER WORKAROUND :
        There is a workaround that may work in some cases, and that
        is to reference the members and methods using the "top"
        member that is available to all windows.
        frame2.eval("top.Frame2.body.style.backgroundColor='green'")
        works, but that's not a good solution and also performs bad
        because of the unnecessary lookups of "top" and "Frame2".

        This approach starts to get silly when you create JavaScript
        objects:

        top.Frame2.myObject=new
        top.Frame2.MyObject(top.Frame2.MY_CONSTANT,
        top.Frame2.document.getElementById('test'));
        (Review ID: 146414)
        ======================================================================
        ###@###.### 10/4/04 18:08 GMT

              djayaramsunw Devananda Jayaraman (Inactive)
              gmanwanisunw Girish Manwani (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: