0

Imagin you have an url (https://www.google.fr/)
You wan't to have the HTML code of this page after the execution of the JavaScript of this page.
Imagin the basic HTML before javascript execution is this :

<html>
    <head>
        <title>Super test</title>
    </head>
    <body>
        <script>
            var i = document.createElement("div");
            i.className = "test";
            document.bodu.appendChild(i);
        </script>
    </body>
</html>

What i need is a code (a way) to get this result :

<html>
    <head>
        <title>Super test</title>
    </head>
    <body>
        <div class="test"></div>
        <script>
            var i = document.createElement("div");
            i.className = "test";
            document.body.appendChild(i);
        </script>
    </body>
</html>

I tryed this options :
- stackoverflow (But did not succeed to use it)
- PHP PhantomJS (But when i'm trying to use it, it give me the code before javascript execution).

Community
  • 1
  • 1
Antoine Duval
  • 342
  • 3
  • 20
  • What you are looking for Antoine is actually an Ajax based solution which may require pairing with a server side solution. You can query a external resource with ajax now, though I'm not sure about the "same domain policies", this days. A solution would be to implement a server side solution using something like cURL which can retrieve you the contents of x URL, and you can serve it back with javascript. – MarkSkayff Oct 18 '15 at 22:19

2 Answers2

0

You need to wait for the DOM to be ready. https://api.jquery.com/ready/

And use body instead of bodu :)

  • Thx, but if i give you an URL, an external URL like google.fr, how can i use your ready function ? I can't put it in an iframe and wait it's ready. We can't get an iframe content of an external URL :'( – Antoine Duval Oct 18 '15 at 20:43
  • Okay, then I don't get it. :( Your code just simply creates and appends a new element to the end of your body. And it works well. Do you want to copy the content of an external page into your page? – Attila Komlosi Oct 18 '15 at 21:17
0

If you want to see the html of the document after the javascript is executed, use document.documentElement.innerHTML.

Try this, i added setTimeout in your original page which opens up alert box and displays you the modified page with "test" div.

<html>
    <head>
        <title>Super test</title>
    </head>
    <body>
        <script>
            var i = document.createElement("div");
            i.className = "test";
            document.body.appendChild(i);
        </script>
  <script>
  setTimeout(function() {
  alert(document.documentElement.innerHTML);
  },2000);
  
  </script
    </body>
</html>
  • Thanks for your help, but i think you don't understand, i don't need the code of MY page, but of an external page, of an external server. Like Google. – Antoine Duval Oct 18 '15 at 21:20