I usually include javascript like this:
<script type="text/javascript" src="js/somejs.js">
is there a way like this :
<script type="text/javascript" src="js/somephp.php?js=somejs">
So that the PHP Script echoes a custom generated string or file of requested name
If this is possible how should be the basic php syntax?
Thanks in advance.
Yes, it’s possible. The
scripttag doesn’t care whether the URL it’s pointed at ends with ‘.js’ or not. If you point it at some URL that will serve valid javascript, then you’re all good, whether that URL ends in ‘.js’ or ‘.php’ or anything else, and whether the mechanism for serving it is your apache server serving a file straight from the file system, a PHP file echoing out the javascript, or anything else that you please.If you do decide to serve JavaScript by echoing it out via PHP, to be neat and correct you should set the content type header to ‘application/javascript’ by calling
header('Content-type: application/javascript');respectively.