0

I want to retrieve a value of a javascript section in a PHP file with my application in Java.

The PHP page contain something like :

<script type="text/javascript">
var tab= new Array();
    tab[0] = "value0";
    tab[1] = "value1";
    tab[2] = "value2";
</script>

I'm using jsoup for parsing the HTML tag. I tried to use Rhino but I don't find example.

Context context = Context.enter();
Scriptable scope = context.initStandardObjects();
Object result = null;
Reader reader = new InputStreamReader(inputStreamOfThePage);
result = context.evaluateReader(scope, reader, "page", 1 , null );
Scriptable varValue = (Scriptable)scope.get("tab", scope);
String valueStr = (String)varValue .get("tab[0]", varValue );

It's giving me the exception : java.lang.ClassCastException: org.mozilla.javascript.UniqueTag cannot be cast to org.mozilla.javascript.Scriptable

I don't know how to cast the object. Maybe there is a better way to do what I want.

Thanks

2
  • Are you trying to get the value after the page has been served or before? Are you trying to get a value from another site's page or do you have access to the source? It is unwise to depend on values extracted from other websites unless you are certain they will change predictably. Commented Jul 7, 2013 at 5:28
  • Thanks,I'm saving the page and after I try to get the value. I'm sure that the value is in the saved page. Commented Jul 9, 2013 at 0:55

1 Answer 1

0

Jsoup is not suitable to parse Javascript... It's normal it doesn't work !

2
  • Welcome to SO. Can you please explain you answer a bit more? Commented Jul 8, 2013 at 16:08
  • Thanks, I'm not using Jsoup for parsing Javascript, only HTML. I would like to use Rhino for parsing the Javascript. Commented Jul 9, 2013 at 0:55

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.