在Java中运行JavaScript [英] Run JavaScript in Java

查看:79
本文介绍了在Java中运行JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javaScript标记:

 <! - 开始ParsTools.com祷告时间代码 - > 
< script type =text / javascriptlanguage =javascriptsrc =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>
<! - 结束祈祷时间代码 - >

我想在java中运行此脚本并接收服务器发送的html文档作为响应。我怎样才能做到这一点?我还需要解析收到的文档并提取一些特殊的标签。



谢谢。

解决方案

  WebClient htmlunit = new WebClient();我们知道最简单的方法就是使用HtmlUnit。 
HtmlPage page = htmlunit.getPage(http://www.google.com);

//执行javascript并获取新页面
page = page.executeJavaScript(< JS code here>)。getNewPage();

更多信息: http://www.aviyehuda.com/2011/05/htmlunit-a-quick-introduction/


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.

解决方案

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/

这篇关于在Java中运行JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆