WebRenderer has two effective methods used for communicating between
Java and JavaScript code. The methods
available consist of the executeScript method
and the executeScriptWithReturn method found
in IBrowserCanvas.
executeScript: - executeScript takes a String containg any valid JavaScript which
will be immediately executed on the page
loaded in WebRenderer. Using this method
you can directly call global or custom JavaScript
functions.
Example: //Changing the current documents background color to green browser.executeScript("document.body.style.backgroundColor='green';"); //Throwing
a JavaScript Alert dialog box browser.executeScript("alert('Background
color changed');");
executeScriptWithReturn:
- executeScriptWithReturn returns any variable from JavaScript or method
return values that you choose to call it
upon.
Example: //Executing
some JavaScript - Calling a function in
JavaScript that //has a return value
and passing the return value back to Java. System.out.println(browser.executeScriptWithReturn("document.title"));
The
executeScriptWithReturn can be called on
any JavaScript function or variable and
it will return the value.