PHP parse a specific .js file.

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

PHP parse a specific .js file.

Post 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. :P Thanks for reading, and I appreciate any help with this.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP parse a specific .js file.

Post by Weirdan »

Code: Select all

 
<Files foo.js>
     ForceType application/x-httpd-php
</Files>
 
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: PHP parse a specific .js file.

Post 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. :?
timwhitlock
Forum Newbie
Posts: 1
Joined: Thu Feb 12, 2009 3:36 pm

Re: PHP parse a specific .js file.

Post 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/
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: PHP parse a specific .js file.

Post by Weirdan »

Come on, this thread is 1 year old... stop necro-digging :lol:
Post Reply