Run JavaScript in Java

html, java, javascript

Solution

The easiest way I know is using HtmlUnit.

WebClient htmlunit = new WebClient();
HtmlPage page = htmlunit.getPage("http://www.google.com");

//executing the javascript and getting the new page
page = page.executeJavaScript("<JS code here>").getNewPage(); 

more info: http://www.aviyehuda.com/2011/05/htmlunit-a-quick-introduction/

Problem

I have a javaScript tag: ``` <!-- Begin ParsTools.com Prayer Times Code --> <script type="text/javascript" language="javascript" src="http://www2.parstools.com/oghat/oghatwhite.php"></script> <script language="javascript"> var CurrentDate= new Date(); var JAT= 1; function pz() {}; init(); document.getElementById("cities").selectedIndex=12; coord(); main(); </script> <!-- End Prayer Times code --> ``` I want to run this script in java and receive the html document that server sends in response. How can I do this? I also need to parse the received document and extract some special tags. thank you.

Original source