views:

305

answers:

4

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 JS "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.

+7  A: 

Put 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.

musicfreak
I think you mean AddHandler and not AddType. AddType sets the outgoing mimetype header.
BipedalShark
Oops, you're right, thanks for correcting me. I haven't done this in a while. :)
musicfreak
"although this makes PHP parse all JavaScript/CSS files on the server" doesn't have to. just stick it in a <Location> or <Directory> tag for that dir your serving the php js files from.
nategood
Oh, you're right, didn't think about that. Edited.
musicfreak
+1  A: 

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 javacript, 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>
Roy Rico
if you do use this approach, make certain to add the appropriate "Content-type" header.
nategood
+7  A: 

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.com 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.com", ...
}

the rest of their js is found in external files.

nategood
I reformatted the code for the sake of not having to scroll to read it.
musicfreak
+1 you really should allow users to cache as much as possible
rojoca
Good answer. From a design perspective, this allows for good MVC separation and encapsulation +1
dreftymac
+1  A: 

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

meridimus