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.
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.
Share Improve this question edited Jun 23, 2011 at 23:48 Bill the Lizard 406k212 gold badges573 silver badges891 bronze badges asked Jun 11, 2009 at 22:06 dreftymacdreftymac 32.4k26 gold badges124 silver badges188 bronze badges4 Answers
Reset to default 7Put an .htaccess
file in your /js/
folder and add the .js
extension to PHP, like this:
AddHandler application/x-httpd-php .js
In other words, have PHP parse all .js
files 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 .css
extension, obviously.
Note: I've never tried doing this in a separate .htaccess
file. If it doesn't work, just put it into your global Apache config.
From my experience, rarely do you need to (and rarely should you) generate an entire script dynamically. For example, in javascript you may need to dynamically get some piece of data (like user info or settings) into javascript, but the rest of the script (classes/functions/DOM manipulations) is static across all users.
Typically in this case you would just want to put the dynamic stuff "inline", output dynamically from PHP and then include the js (the 95% that doesn't need dynamically generated) as an external script. The most obvious reason for this is caching the js/css.
Consider how reddit. does it by looking at their source code for getting user data into javascript.
var reddit = {
/* is the user logged in */ logged: 'username',
/* the subreddit's name (for posts) */ post_site: "",
/* are we in an iframe */ cnameframe: false,
/* this page's referer" */ referer: "",
/* the user's voting hash */ modhash: 'lzbcszj9nl521385b7e075e9750ee4339547befc6a47fa01e6',
/* current domain */ cur_domain: "reddit.", ...
}
The rest of their js is found in external files.
You could just use mod_rewrite to make certain php files be seen as CSS/JS
e.g. /css/screen-style.css points to css.php?friendly_id=screen-style
You can use .php files in JavaScript and CSS calls. It's not pretty and anyone looking at your source knows it's a script, but it saves the hassle of configuration on the server. Also, if you're making dynamic JavaScript, I would suggest adding a timestamp on the end so the browser doesn't cache it.
Example.
<script src="myjavascript.php?a=20090611-021213"></script>