Jump to content

Add in JS with PHP


unemployment

Recommended Posts

I currently add in each pages JS in my site by using a variable

 

Example: 

 

if (file_exists("./assets/js/pages/${page}.js"))

{

echo "<script type=\"text/javascript\" src=\"./assets/js/pages/${page}.js\"></script>";

}

 

If I'm on list.php for instance it would load the list JavaScript, but based on which if statement result is executed, some of the JavaScript is not need.  How can I still use this format but split up my JavaScript functions to correlate with the correct page loads.  I'm tired of looking at these firebug errors.

Link to comment
https://forums.phpfreaks.com/topic/227903-add-in-js-with-php/
Share on other sites

either split the js into different files or use PHP if/then around the js functions and load them as needed. the second option will require that the JS is in the .php file OR the .js file is changed to a .php file so that it is executed by the server before being sent to the browser.

Link to comment
https://forums.phpfreaks.com/topic/227903-add-in-js-with-php/#findComment-1175181
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.