sajad
sajad

Reputation: 2174

Run JavaScript in Java

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.

Upvotes: 2

Views: 2325

Answers (2)

look4chirag
look4chirag

Reputation: 453

Sajad/Future visitors, You may look into Jakarta Bean Scripting Framework (BSF) to run your javascript even as standalone code if using older java version (< 1.6, later versions of java have it embedded)

Upvotes: 0

Avi Y
Avi Y

Reputation: 2495

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/

Upvotes: 7

Related Questions