How to output DOM after loading javascript?

As a title, my question is how to output (say, save as a text file on the server or pass the result of some other php function using ajax) all the contents of the DOM on the page?

I did some homework, I tried curl, it can just output all the contents of the DOM using "curl http://google.ca > dom.txt" However, this approach will not save the content generated by Javascript, in other words, javascript code will not work . Another approach is to insert javascript code into the page and allow the page to be uploaded to the website we want to display, and then use javascript code to save the entire DOM file after loading.

I'm not sure if phantom.js can do such a task, if so, how?

Can any body give a detailed answer on how to achieve this?

I am open to any solutions, this program will run on my server to provide the service.

Thanks in advance.

+3
source share
3 answers

Why not:

jQuery(document).ready(function($) {
    $.post(
        '/your_filename.php',
        'html='+$("html").html(),
        function(response){
            alert(response);
        }
    );
});
+4
source

You can get the content of an HTML element (including head and body) with document.documentElement.innerHTML. If you need everything, you can combine document.doctypewith document.documentElement.outerHTML.

Please note that it is outerHTMLnot a cross browser (it works in IE and Chrome, but not in Firefox) - in order to simulate outerHTMLfor Firefox, see this question: How to make OuterHTML in firefox?

+1

Javascript - , . PHP DOM, dom , .

, , , , : , ? , , , Brilliand iambriansreed, dom Javascript/jQuery.

0

All Articles