JSObject and JSExceptions

March 20, 2006

If 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() +"\");"));
}
This does work with out any trouble on Safari. Posted by raditha at March 20, 2006 8:20 AM
Your Ad Here

 

Jabber  |  Linux  |  mySQL  |  PHP  |  Java  |  Site Map  |  Wiki

Downloads  |  About  |  Links  |  Contact  |  Home

 

Copyright © Raditha Dissanayake 2003 - 2007

Terms of Use  |  Privacy

 

 

March 2006
Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31