1

I am really eager to know , is there any API or way to achieve this.I want to get Source code generated by the Browser.

In below code initial CSS Class of colorElement is redFontClass but onload javascript done changes to blueFontClass.Which is very clear using the Firebug.I want to get this kind of Response using URL calling in Java. Is this possible ?.I don't want any of the Javascript but want finally browser generated HTML with CSS which is very useful for Flying-Saucer kind of PDF generators.They are not supporting for the Javascripts for time being.

Real HTML :

<html>
<head>
<style>
.redFontClass
{
  color : red;
}
.blueFontClass
{
  color : blue;
}
</style>
<script language="javascript"> 
function changeColor()
{
    document.getElementById('colorElement').className='blueFontClass';
    alert("asdfsd");
}   
 </script>
</head>
<body  onload="javascript:changeColor();">
<b>This Should come as <span class = "redFontClass" id="colorElement">Red</span> </b>
</body>
</html>

FireFox Firebug Image :

enter image description here

Update : I need the Browser generated HTML if I call the URL of that particular file. Because I will call this from server side Java Code not using any clinet side applications like Browsers.

Browser Generated HTML :

<html><head>
<style>
.redFontClass
{
  color : red;
}
.blueFontClass
{
  color : blue;
}
</style>
<script language="javascript"> 
function changeColor()
{
    document.getElementById('colorElement').className='blueFontClass';
    alert("asdfsd");
}   
 </script>
</head>
<body onload="javascript:changeColor();">
<b>This Should come as <span id="colorElement" class="blueFontClass">Red</span> </b>

</body></html>
sunleo
  • 10,589
  • 35
  • 116
  • 196

3 Answers3

0

You could just post the source of the page back to a server-side script.

In jQuery you could have a function such as:

function postGeneratedSource() {
    var data = $('body')[0].outerHTML;
    $.post('/path/to/script', data);
}

If you didn't want the JS then you could either change body for a relevant container or otherwise strip the script tags on the server side.

RichieAHB
  • 2,030
  • 2
  • 20
  • 33
0

You can get whole HTML code generated by browser by using this code:

var source = '<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>';
Eltu
  • 470
  • 4
  • 11
0

You gen use following code to get the html.

        String url = "http://www.google.com/search?q=developer";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        //add reuqest header
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", "Mozilla");


        BufferedReader in = new BufferedReader(new   InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        //print result
        System.out.println("hi"+response.toString());
Rks
  • 103
  • 2
  • 13