Background: When generating HTML content with PHP or any such thing, it is possible to encapsulate links to JavaScript and CSS inside tags without actually having to include the CSS and JavaScript “in-line” with the rest of the content. All you have to do is create a link to the file.
Example:
{script type=”text/javascript” src=”./js/fooscript.js”}{/script}
Question: The above approach does not work, however, if your PHP needs to dynamically generate some or all of your JavaScript code. Is there a way to have a clean “one-line” link as above, but still use dynamically-generated JavaScript?
Obviously, one way to do it is to have PHP auto-generate the JavaScript and write that to a file; however that approach is undesirable for various reasons. I am wondering if there is an alternate trick to doing this that I have not thought of yet.
Put an
.htaccessfile in your/js/folder and add the.jsextension to PHP, like this:In other words, have PHP parse all
.jsfiles as PHP files. So your scripts would really be PHP files on the server-side that output JavaScript. Do the same for stylesheets, only use the.cssextension, obviously.Note: I’ve never tried doing this in a separate
.htaccessfile. If it doesn’t work, just put it into your global Apache config.