0

I've a html page with a lot of css and javascript tag in its head. I want to print them in a php page as it is. I've tried php echo file_get_contents("html_url"); and fread function also . The php page loads the html as expected. But the problem is some css code and javascript code does not work. For example I've a javascript fuction this:

window.addEventListener('load'      , openFirstCollapse  );

function openFirstCollapse(){
 
  var elem = document.querySelectorAll('.card-link');
  elem[0].click();
  
}

does not work and most of the javascript function does not work. I think the <script src="javascriptfile.js" tag in the head of the html page is unable to load it through php function. Can someone tell me what is actually happening here ? And how can I get rid of it ?

Koushik
  • 41
  • 4
  • "I think"...you can verify it easily with your browser's network tool. If you're loading this html from another server then obviously it won't work because the is file is on that server, not yours, but the link is relative, so when it hits your browser, embedded in _your_ page, the browser tries to download it from your server not the original one (of which it has no knowledge) – ADyson Nov 23 '20 at 12:24
  • Always debug your scripts with enabled [PHP Error Reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)! Is there any PHP error printed? Is the content accessible for your PHP webserver? And actually what are you trying to achieve - print the source code or raad the source and print actual web page? – ino Nov 23 '20 at 13:29

1 Answers1

0

file_get_contents() read a file as a string. See this from php.net to know details about file_get_contents() Now if you want to fetch page like https://stackoverflow.com, you can just use this PHP code:

echo file_get_contents("https://stackoverflow.com/");

Already there is a solution related this problem contains in stackoverflow from 11 years ago! Check out this for details How do I get the HTML code of a web page in PHP?