Using a javascript url with showdocument()

I have an Applet that needs to run on the following platforms: Internet Explorer (Windows-NT, Windows -2000), Netscape (Windows-NT, Windows-2000, Sun(Solaris), RedHat(Linux), HP(11.0), IBM(AIX)).

The Applet needs to call a JavaScript function that is included on the HTML page that contains the applet.

In JRE-1.3.1 I used the AppletContext.showDocument(url,"_self" ) where the url is "javascript:MyScript('param1')". This works just fine. I tried using showDocument without the second parameter and all the other choices for the second parameter and none of them worked (nothing happened).

I tried the some code with JRE version 1.4-beta. It seems that version 1.4-beta no longer supports a URL that starts with "javascript" in the AppletContext.showDocument(url,"_self" ) method. The error is:

netscape.javascript.JSException: call does not support self.open

at sun.plugin.javascript.navig.Window.call(Unknown Source)

at sun.plugin.ActivatorAppletContext.showDocument(Unknown Source)

I tried to catch the JSException but I cannot seem to.

I then tried a completely different approach. I then grabbed the JSObject for the page and tried to call my javascript function (MyScript) directly:

JSObject theObj = JSObject.getWindow(this);

Object paramArray[] = new Object[1];

paramArray[0] = Param1;

theObj.call("MyScript", paramArray);

This worked fine on Netscape[Windows-NT, Windows-2000]. HP throws a JSException on getWindow() and Solaris just hangs. I did not even try IE.

What is the recommended approach here?

[1612 byte] By [jwatrous] at [2007-9-26 2:37:43]
# 1

I have gotten this to work using the Java plugin 1.3.0.0.2

You have to initialize a JSObject in the init() method of the applet.

Then when you use the JSObject use .eval(String) instead of .call (I couldn't make this work either) beacause they effectively do the same thing! (invoke a function)

Call .eval with the parameter String you would use for the JavaScript function call (if you have multiple arguments you'll probably have to tokenize them - sorry)

On caveat - if you have the character " in your argument String you have to escape it

and if your String is a URL you have to double escape (i.e. "\\\"") the " value (once for java once for Javascript - otherwise it will appear as though nothing happened)

I hope this helps

lordkilgour at 2007-6-29 10:07:28 > top of Java-index,Archived Forums,Java Programming...