JSObject and JSExceptionsMarch 20, 2006If you tried out read my article on java and javascript interaction and then tried out the demo you will probably see the expected result. The sample code provided is compatible with IE, Firefox and Safari. However if you tried to call javascript methods from your applet while using the Safari browser there is a chance that you may not see the expected result. That is because of what appears to be a bug in the Safari browser. The following code ...
public void init()
{
jso = JSObject.getWindow(this);
}
...might produce an exception.
netscape.javascript.JSException
at netscape.javascript.JSObject.getWindow(JSObject.java:145)
There is no reason for it to fail. What's annoying is that sometimes it works sometimes it does not. So the work around is to enclose the JSObject.getWindow() call in a try - catch block. And then to modify the code that calls the javascript method as follows:
if(jso != null ) {
try {
jso.call("updateWebPage", new String[] {txt.getText()});
}
catch (Exception ex) {
ex.printStackTrace();
}
}
else
{
getAppletContext().showDocument(
new URL("javascript:alert(\"" + txt.getText() +"\");"));
}
|
|



