views:

400

answers:

3

Instead of automatically following JavaScript redirects, can I force HtmlUnit to return the URL the JavaScript wants to redirect me to?

// context:

If there's 5 JavaScript redirects in a row, I can only see the URL of the page where it stopped - I can't view the 5 URLs it ran through.

Perhaps I can hook up the logger to a callback method to look for redirects? Not sure if this is possible, or how it would work..

A: 

Unfortunately, HtmlUnit is a Java based HTML parser, not a Java based JavaScript interpreter. You'll need to filter all "raw" JavaScripts out using HtmlUnit and then run them with help of a JavaScript interpreter, like Rhino in the javax.script API. Here's a tutorial about that.

Edit: HtmlUnit actually contains a wrapper around Rhino as described here. But yet, you have to handle the JavaScripts individually yourself.

That said, if you elaborate a bit more about the context for which you need to do this, then we may be able to provide better -but probably completely different- answers/suggestions for the particular functional requirement.

BalusC
Thanks for the reply. It's a bit hard to describe, but I added a sample scenario to provide some context.
Marco
A: 

Previous answer is quite inaccurate :-(

I think that what you want to do is possible but you should use HtmlUnit mailing list to discuss it as it may require a bit hacking.

Marc Guillemot
+1  A: 

You can do this by adding a WebWindowListener to the WebClient, and waiting for the webWindowContentChanged method to get called.

James Kingsbery