Skip to content Skip to sidebar Skip to footer

How To Talk To A Javascript Function From Swt

My HTML file has a javascript function xxx_return(), which will return a string value. Is there any way i can take this value from Java layer?. I am using SWT shell to display this

Solution 1:

Use an SWT Browser object. Then you can simply use String result = (String)Browser.evaluate("xxx_return();").

Solution 2:

I found it, the exception occurred since the Browser.evaluate() was getting called before the page was loaded in the shell. I added a ProgressListener to know the completion, and tried calling it worked.

browser.addProgressListener(newProgressListener() {
              publicvoidchanged(ProgressEvent event)
              {

              }
              publicvoidcompleted(ProgressEvent event)
              {String htm;
                htm=(String)browser.evaluate("return returnHTML()"); 
                System.out.println(htm);
              }
            });

Thanks All

Solution 3:

In addition the above solutions, add "return" in front of the expression. Also depending on what you are evaluating, completed event can be ignored. Following expression just works.

browser.evaluate("return 4 + 5;")

Of course if you're evaluating javascript from the page loaded in the browser, evaluate must be called after completed event, otherwise the javascript may not have been loaded.

Post a Comment for "How To Talk To A Javascript Function From Swt"