How can you get the source code of the pages (after running javascript) using PHP?

On my page, javascript adds many classes to load the page (depending on the page). How can I wait for javascript to add these classes and then get the HTML using Javascript or PHP from another file?

+4
source share
5 answers

When the page finishes loading, send the resulting text back to the PHP script using Ajax.

$(function() { var data = $('body').html(); $.post('/path/to/php/script', data); }); 

(This example assumes you are using jQuery)

+2
source

Sounds like you need Firebug . If you use Google Chrome, you can also use the Google Chrome Developer Tools .

These tools will allow you to view live DOM pages, as well as track any changes made by your javascript. Such tools are necessary for us as developers.

+1
source

You cannot get the provided HTML source by a resource other than JavaScript on your page. After JS has finished all the content changes in HTML, you can place the HTML source in PHP on the server and save it.

Pseudocode:

 // JavaScript using jQuery setTimeout("jQuery.post('/catch.php', jQuery(document));", 2000); // on the server side create a catch.php file <?php file_put_contents('./tmp.txt', 'php://input'); 
+1
source

You cannot, easy.

JavaScript changes the DOM in memory. This is a completely separate entity than the “source” that you originally sent to the browser.

The closest thing you can do is build the XML representation of the DOM through JS and send it back to the server via AJAX. Why did you want / should do it, outside of me.

0
source

Open your bookmarks / Favorites and create a new one with this, and then click on it after the page loads:

 javascript:IHtml=document.documentElement.innerHTML; LThan=String.fromCharCode(60); LT=new RegExp(LThan,'g'); IHtml=IHtml.replace(LT,'&lt;'); IHtml=IHtml.replace(/ /g,'&nbsp; ');Out ='';Out+='<!DOCTYPE html PUBLIC "-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN"';Out+=' "http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd">'; Out+='<html xmlns="http:\/\/www.w3.org\/1999\/xhtml" xml:lang="en-US" lang="en-US">'; Out+='<head><title>Inner HTML<\/title><\/head>'; Out+='<body style="color:black;background-color:#ffffee;">'; Out+='Body HTML:<br \/><ul>'; NLine=String.fromCharCode(10); ILines=IHtml.split(NLine); for (ix1=0; ix1< ILines.length; ix1++) { Out+='<li>'+ILines[ix1]+'<\/li>'; } Out+='<\/ul>'; Out+='&nbsp;&nbsp;[<a href="javascript:void(0);" onclick="window.close();" title="close">Close<\/a>]'; Out+='<\/body><\/html>\n'; PopUp1=window.open('','IHTML'); PopUp1.document.write(Out); PopUp1.document.close(); 
0
source

Source: https://habr.com/ru/post/1341854/