views:

193

answers:

3

Hi,

i m getting a web page content in a string by using java.net.URL and Input Streams.

the problem i m having that my javascript is not rendering and i m getting the as it is response.

<html>
 <head></head>
 <body>
  <script>
   document.write("<h1>hello world!</h1>");
  </script>
 </body>
</html>

suppose this is the code written in HTML file .. now i want when i get the webpage content in string .. the javascript should b rendered..

<html>
 <head></head>
 <body>
   <h1>Hello World!</h1>
 </body>
</html>

like this .. how i can do that ????

+1  A: 

The JavaScript won't be executed automagically by java.net.URL. The HTML file with the inline JavaScript is just seen as any other plain text file and isn't parsed in any way. No DOM is built, no JavaScript is executed, no CSS applied. What you need to do to get the JavaScript executed is to parse it with something like HtmlUnit.

If you have control of the HTML, though, I'd recommend you to just replace the document.write() statements with static HTML. A very rudimentary alternative is of course to just do a search on the regular expression document\.write\(([^\)]+)\);? and replace it with the first captured match.

asbjornu
A: 

Check out Jaxer

erikkallen
A: 

https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/LiveConnect/JSObject

The eval method in the mentioned netscape.javascript.JSObject class might be what you're looking for (though I'm not sure if it takes care of DOM manipulation and if it works across browsers).

Good luck!

Abhishek
"Eval" is not enough i think. It may render the js between "script" tags but it can't fires the events like "body onload='blabla'" etc.
Ahmet Kakıcı