views:

434

answers:

1

I am getting Access Denied exception when I am calling a Java Script Function from Java Applet only in IE. ( modified my original question with updated information.)

Here is my HTML code

<script type="text/javascript">
   function uploadComplete() {
       alert("in Upload Complete");
       ju.doneUpload(true);
   }
</script>

ju is declared globally in the same page which calls the doneUpload from a different JavaScript file. I have included MAYSCRIPT in my applet Tag.

Java Code [After adding AccessController] :

 AccessController.doPrivileged(new PrivilegedAction<Object>() {
 public Object run() {

JSObject scriptObject = getScriptObject(uploadDialogBox.applet);
if(scriptObject != null) {
    try {
        // this is the call where it   throws an exception
       **scriptObject.call("uploadComplete", null);**
    } catch(JSException e) {
        System.out.println("exception " + e.getMessage() 
            + " WrappendException " + e.getWrappedException()
            + " stack trace " + e.getStackTrace());
    }
}
return null;
}

});

private JSObject getScriptObject(JApplet appletInstance) {
    JSObject result = null;
    // JSObject doc = null;
    try {
        result = JSObject.getWindow(appletInstance);
        // doc = (JSObject) result.getMember("document");
    } catch (JSException e) {
        System.out.println("Exception in getScriptObject : " + e.getMessage()
            + " Wrappend exception " + e.getWrappedException());
    }
    return result;
}

It throws an JSException: Access is denied

The alert function in uploadComplete is not called. What am I doing wrong here?

+1  A: 

Does this help?

A summary from that answer on the Sun forums:

Hello, I had same problem. I solved it, you just need to implement applets method like this:

public void f() {
   AccessController.doPrivileged(new PrivilegedAction<Object>() {
      public Object run() {
         // do something
         return null;
      }
   });
}

The only problem with this approach is that when you call method for the first time, it takes pretty long time to respond (4-5s), and every next time, response is immediately.

Edit: OK, a second potential solution is to use getDocument() as described in this article. One word of warning, in our testing, we have seen that this is not always reliable on Firefox on the Mac. The essence of this method is to do:

getAppletContext().showDocument(new URL("javascript:uploadComplete()"));
Paul Wagland
It still doesn't help ... I wrapped AccessController over my code and still get Access is Denied exception. Should I check applet.policy or crossdomain.xml ?
Some_coder
I had showDocumnet before URL url = new URL("javascript:uploadComplete()"); uploadDialogBox.applet.getAppletContext().showDocument(url, "_self"); but it doesn't work in IE but works in FF. So was trying liveconnect as an alternative.
Some_coder
Actually, after wrapping my code over AccessController it works in FF but doesn't for IE. So comes down to a browser issue ? It gives the "Access is Denied." when scriptObject.call("uploadComplete", null); is called.
Some_coder
My guess is that this is a browser issue, and that your IE is probably locked down and won't allow external access to the DOM.
Paul Wagland