This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes Calling Javascript method in js file from Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Calling Javascript method in js file from Applet" Watch "Calling Javascript method in js file from Applet" New topic
Author

Calling Javascript method in js file from Applet

T Almo
Greenhorn

Joined: Sep 09, 2009
Posts: 10
I am attempting to call a javascript function (residing in a js file) from an applet to update a ProgressBar element. My method updateProgressBar() is located in data.Import.js and I include this file in the html page, which is why I dont understand why I am getting a "updateProgressBar function is undefined" error message. Is there some extra something Im supposed to put in the URL to let it know to look in my data.Import.js for the function?

In HTML:
<script src="data.Import.js" type="text/javascript">

In Applet:


I tried stuff like new URL("javascript:data.Import:updateProgressBar()"); but I got an invalid label error. I was also sure to put the MAYSCRIPT and scriptable parameters on my applet. My applet is setup as follows, and may be the cause of the function not found: initial applet class instantiates a thread class, which then instantiates and runs my upload class. This upload class is what is needs to call the javascript function.

I have also tried using the LiveConnect methods such as:

But I haven't had any success with that either, as I get this error message in the Java Console: "No such method "updateProgressBar" on JavaScript object" Previously, this main upload class opened a new window, so that may be causing it to have a different document/window object, but I am changing it so that it no longer opens a new window.

Any help would be greatly appreciated.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
Are you setting the MAYSCRIPT attribute in the applet tag?


Ping & DNS - my free Android networking tools app
T Almo
Greenhorn

Joined: Sep 09, 2009
Posts: 10
Yes, I am using the MAYSCRIPT attribute, as I mentioned above.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
Ah, I missed that. The LiveConnect code works fine for me if I put it in the paint method. It seems that the web page isn't fully loaded and initialized if the code is in the init method.


T Almo
Greenhorn

Joined: Sep 09, 2009
Posts: 10
Is it possible that the newFrame I create and pass to the FileDialog is what my JSObject window and document is referring to? My thought is that either the JSObject isnt referring to the right thing, or the threading I described above has something to do with it.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
I'm not sure what the "myApplet" parameter is that you were using in the getWindow call, but if you use "this" like in my code then the JSObject should refer to the enclosing web page.
T Almo
Greenhorn

Joined: Sep 09, 2009
Posts: 10
The myApplet parameter is the Applet. I have to use that parameter because this window.call function runs in a child class of the main class that extends Applet(the threading setup I described in the first post). I pass the applet itself to the subclass.
T Almo
Greenhorn

Joined: Sep 09, 2009
Posts: 10
Just had an interesting observation. I made a separate, simple test page similar to yours, and when the updateProgressBar function was only an alert, it worked fine, but if I changed it to document.write(s), I got the same 'JSException: No such method "updateProgressBar" on JavaScript object' I think this is related to the threading my program implements. All I am trying to do is update the progress bar on an ExtJS element, so its apparently the same reason document.write is failing.

I dont have complicated threading at all. As I described earlier, all that happens is my main applet class calls an uploadThread class, which then starts the upload class in a new thread, and that new thread runs the whole time until its done.
T Almo
Greenhorn

Joined: Sep 09, 2009
Posts: 10
I solved it. The problem was that the js file in which the function resided was an ExtJS class, so I had to get at it with the following applet code:


When I created my ExtJS component, I gave it the id myUploadWindow, so I could grab it with the get command. Thank you for your help.
 
Consider Paul's rocket mass heater.
 
subject: Calling Javascript method in js file from Applet
 
Similar Threads
JSObject, how can I call JS function from Java Applet ?
Passing Info Out of an Applet
JSException: Access is denied.
Applet to Javascript Communication
Problem calling javascript from an applet in NS 6