2

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.

1
  • 5
    I suggest you take a look at Rhino Commented Apr 8, 2012 at 5:46

2 Answers 2

7

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/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you; In this way I always have to first load a page(for example www.google.com); then execute my javaScript code in that html page. Is there any cleaner way to run javaScript code without need to this stage?
Use file:///index.html and have a empty file locally.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.