1

I am using JSObject to invoke a Javascript function from my Applet.

SSCCE of my Applet:

package ch.vrag.web;

import java.applet.Applet;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;

import netscape.javascript.JSObject;

public class Sscce extends Applet {

private static final long serialVersionUID = -5403444704039831864L;

public void init() {
    try {
        Policy.setPolicy(new Policy() {
            Permissions perms = new Permissions();

            public PermissionCollection getPermissions(CodeSource codesource) throws SecurityException {
                AllPermission grant = null;
                grant = new AllPermission();
                perms.add(grant);
                return perms;
            }

            public void refresh() {
                // Do nothing
            }
        });
    }
    catch (Exception e) {
    }

    JSObject window = JSObject.getWindow(this);

    JSObject phoneNums = (JSObject) window.eval("getWindowRectangle()");

    int x;
    int y;
    int width;
    int height;

    if (phoneNums.getSlot(0) instanceof Integer) {
        x = (Integer) phoneNums.getSlot(0);
        y = (Integer) phoneNums.getSlot(1);
        width = (Integer) phoneNums.getSlot(2);
        height = (Integer) phoneNums.getSlot(3);
    }
    else if (phoneNums.getSlot(0) instanceof Double) {
        x = ((Double) phoneNums.getSlot(0)).intValue();
        y = ((Double) phoneNums.getSlot(1)).intValue();
        width = ((Double) phoneNums.getSlot(2)).intValue();
        height = ((Double) phoneNums.getSlot(3)).intValue();
    }
    else {
        return;
    }

    System.out.println("x: " + x + " y: " + y + " width: " + width + " height: " + height);

};
}

An SSCCE of my HTML File:

 <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <script type="text/javascript">
        function getWindowRectangle(){

            var winX = (document.all)?window.screenLeft-2:window.screenX;
            var winY = (document.all)?window.screenTop-2:window.screenY;

            var winWidth = (document.all)?document.documentElement.offsetWidth+4:window.outerWidth;
            var winHeight = (document.all)?document.documentElement.offsetHeight+4:window.outerHeight;

            return([winX,winY,winWidth,winHeight]);
        }       
    </script>

    <title>SSCCE</title>
</head>
<body>
    this is an SSCCE
    <APPLET CODE="ch.vrag.web.Sscce.class" name="SSCCE" codebase="scripts" archive="Sscce.jar" width=0 height=0 MAYSCRIPT >
    </APPLET>
</body>

This works great exept in Internet Explorer 6. There I have this Java Exception:

java.lang.Exception: netscape.javascript.JSException: Unknown Name

what is this Exception good for? What am I doing wrong?

Neifen
  • 2,546
  • 3
  • 19
  • 31

3 Answers3

1

The Internet Explorer 6 seems to have a Problem with the getSlot() command, but I could use a Workaround.

with this Object:
JSObject phoneNums = (JSObject) window.eval("getWindowRectangle()");

I can get a String out of phoneNumbs (phoneNums.toString();) this String gives me my return values in this form:

"number1,number2,number3,number4"

so I can split the string and get the numbers I need.

The moral of the story is, that Internet Exlorer 6 is not no contemporary and should not be used any more!

Neifen
  • 2,546
  • 3
  • 19
  • 31
0

It has to do with the browser not being to run Javascript properly. Make sure IE6 is patched to the SV3 version and Javascript is enabled along with the security setting possible.

jn1kk
  • 5,012
  • 2
  • 45
  • 72
  • It's Sv2 ... and you think that it could be that? – Neifen May 24 '12 at 13:55
  • @Neifen Seeing how you need to test it on IE6, you need all the help you can get. It is possible IE6 doesn't even support those specific Javascript functions either. – jn1kk May 24 '12 at 14:04
  • the javascript function are not the problem, the problem is the `getSlot` I think... but I have no idea why – Neifen May 25 '12 at 08:52
0

At the bottom of Read/Write HTML field values from Java is the fine print:

For best result, never use LiveConnect JSObject in Applet's init() method.

The reason being it is commonly null at that point. I've had more success obtaining a reference to the JSObject in the applet start() method.


JSObject phoneNums = (JSObject) window.eval("getWindowRectangle()");

I suspect this should be JSObject.call("getWindowRectangle()") instead. Do some debugging on the returned Object, inspect it in a debugger to check the content is as you expect.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • same error. I tried to comment code lines out to see where the error is and I did have no error anymore, when i commented the line the `getSlot()` commands out in the Applet... – Neifen May 25 '12 at 08:50
  • hmm it's not better... but when I trace the attribute `phoneNums`I get "123,322,2135,342" (invented Numbers) so I think I could split it instead of making that `getSlot()`thing... – Neifen May 25 '12 at 10:31