process PHP in a .js file?

  • kenw232
  • Born
  • Born
  • kenw232
  • Posts: 2

Post 3+ Months Ago

I have a normal web site with external javascript files as usual. they're included normally with lines like:

Code: [ Select ]
<html>
<script language="JavaScript" type="text/javascript" src="/whatever.js"></script>
</html>
  1. <html>
  2. <script language="JavaScript" type="text/javascript" src="/whatever.js"></script>
  3. </html>


I simply what to add PHP code to the whatever.js file. Now I can't because .js files are not parsed for PHP. I'm under Apache 1.3.41 using PHP 4.4.9.

Is it as easy as changing the AddType for .js? I think I need it to be "AddType application/x-javascript .js". If I make it "application/x-httpd-php" that has problems.

Any ideas how I can do this?

Thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Be careful about having the server parse every single JS file as PHP, it can both lead to unexpected results with existing scripts and add unneeded load if you have a lot of JS that doesn't need to be parsed.

If you really want to have JS parsed as PHP, set aside a directory, similar to a CGI-bin, to retrieve PHP-parsed JS files from.

You're on the right track with AddType, you should be able to copy the AddType you're currently using for *.php files into the new sub-directorys htaccess, and add the *.js extension to that.
  • conanite
  • Novice
  • Novice
  • User avatar
  • Posts: 16
  • Loc: paris, france

Post 3+ Months Ago

You can also write

Code: [ Select ]
 
<script language="JavaScript" type="text/javascript" src="/whatever.php"></script>
 
  1.  
  2. <script language="JavaScript" type="text/javascript" src="/whatever.php"></script>
  3.  


and instead out outputting html from whatever.php, output javascript instead.

If you just need to run some user-specific javascript, you might however be better off including that in your main php file, so that you have

Code: [ Select ]
 
<head>
<script language="JavaScript" type="text/javascript" src="/whatever.js"></script>
<script type="text/javascript">
  // user-specific stuff here
</script>
</head>
 
  1.  
  2. <head>
  3. <script language="JavaScript" type="text/javascript" src="/whatever.js"></script>
  4. <script type="text/javascript">
  5.   // user-specific stuff here
  6. </script>
  7. </head>
  8.  


the user-specific stuff can call functions defined in whatever.js, so your php doesn't get bloated, and your js remains a static resource (which can be cached and served more efficiently)

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 89 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.