Chir
Chir

Reputation: 691

How to use Rhino to execute a JavaScript library and invoke an API on JavaScript library to get HTML

I have a JavaScript library which generate html depending on parameters passed to the library. e.g. The library has a method which accepts id of an html button and the method then wraps html button with html div (and some inline css) to make it look good. The method returns html string representation of the button.

Is it possible to use this JS library with Rhino and call the method by passing some id (which should actually be an id of a button in html page) and get html string representation of the button?

Thanks in advance

Upvotes: 1

Views: 3200

Answers (2)

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

As far as I understand, the Rhino library, when called from within a Java program, has no access on its surrounding HTML, so you can't use a button's ID from the web page. Of course, you could create the HTML code from the scratch, if your library supports this.

If your button is embedded in the HTML page surrounding your applet, you would better use the Javascript bridge to call the browser's Javascript engine.

Upvotes: 0

Adam Ayres
Adam Ayres

Reputation: 8900

Yes, it is possible to load JS libraries into Rhino and use them. You could then either specify the window.location to the html page you or create an element and use innerHTML to put it into the DOM (if you have the HTML as a String). If you are just trying grab a button by its id attribute then you might considering just using document.getElementById and not loading your library. Here is a blog article that talks about loading JS libs like jQuery, Prototype and Mochikit into Rhino:

http://ejohn.org/blog/bringing-the-browser-to-the-server/

Upvotes: 2

Related Questions