Page 1 of 1
PHP parse a specific .js file.
Posted: Sat Feb 09, 2008 6:16 pm
by JellyFish
How can I get my server to have PHP parse a JavaScript file (e.g. foo.js) using an .htaccess file? For example. I have a file called "foo.js". I would like to tell my server, using an .htaccess file, that I want the php parser to parse this file. How would I do this? Sorry for keeping this message a little short but my enter key is not working on Firefox again. It's a bit annoying but I'm going to have to restart my browser.

Thanks for reading, and I appreciate any help with this.
Re: PHP parse a specific .js file.
Posted: Sat Feb 09, 2008 7:18 pm
by Weirdan
Code: Select all
<Files foo.js>
ForceType application/x-httpd-php
</Files>
Re: PHP parse a specific .js file.
Posted: Sat Feb 09, 2008 8:00 pm
by JellyFish
Weirdan wrote:Code: Select all
<Files foo.js>
ForceType application/x-httpd-php
</Files>
Yes this was one of the result I got from google. But doesn't this code just force foo.js to respond to the client with a different mime-type, other then application/x-javascript? When I put his in my .htaccess and open foo.js with firefox, firefox asks me to download this file rather then displaying the contents right on the viewport of the browser like it usually does with javascript files. So this suggest that it's just changing the content-type header of the file rather then having php parse the js file.
EDIT: I'm pretty sure that's all it does; It just forces the files mime-type to be something else. I don't want the mime-type of the file to be anything other then application/x-javascript, application/javascript or text/javascript. What I'm trying to accomplish is a way to tell my server to parse this file with php's parser before it's sent to the clients browser.
I've found that this:
Code: Select all
<Files foo.js>
AddHandler x-httpd-php5 .js
</Files>
makes all files with the basename foo.js to be parsed by php. SO in other words it works. But I would like it to be a more specific file in a specific directory. So I try:
Code: Select all
<Directory /path/>
<Files foo.js>
AddHandler x-httpd-php5 .js
</Files>
</Directory>
But I get a 500 Internal Server Error. Also putting this .htaccess file in the directory I would like to apply this to doesn't work for some other reason.

Re: PHP parse a specific .js file.
Posted: Thu Feb 12, 2009 3:48 pm
by timwhitlock
As nobody will see the URL to your .js file, why not just use a .php file? Perhaps identify it for your own benefit, like foo_js.php?
You'll still need to maintain the JavaScript mime type, but that's easy - just add the following to the top of the file:
Code: Select all
<?php
header('Content-Type: application/x-javascript', true);
If you're interested in parsing JavaScript with PHP, check out these two URLs:
http://jaspa.org.uk/
http://web.2point1.com/2008/05/09/full- ... r-for-php/
Re: PHP parse a specific .js file.
Posted: Thu Feb 12, 2009 4:55 pm
by Weirdan
Come on, this thread is 1 year old... stop necro-digging
