This is actually a combination js, php, htaccess question so it could go in either one of those categories.

I, like every combo server/client/ajax programmer like to mix my php with my js. I'm just curious about something, I know that you can configure .htaccess to refer to new extensions as something else. Actually let me start here.

Many times I will use external js files but will include them using a php include, for example include("phpjs/main.js.php"); which will allow my editor to treat it more like a regular php file rather than a js file. I know it looks dirty if you view source but I never care about that anyway, well actually, maybe I do which is why I am asking this question:

Can I configure apache, through .htaccess to parse a .js file like a .php file. I'm pretty sure it will have no effect on the client, and then I can configure my editor to parse the .js file more like a .php file(for code completion and coloring), and then include it using the standard <script src= include, allowing me to be able to include php in a .js file.

Recommended Answers

All 2 Replies

Its possible but I wouldn't recommend it. If, down the line, you want to add an actually .js file you'd have to either A) change the file extension or B) add another handler. The quickest way to do this is have all your Javascript in a PHP file then at the top of the file define the content type:

<?php header("Content-type: text/javascript"); ?>
function someJavascriptFunction(){
  js1 = <?php echo $blah ?>;
}

There's nothing saying a script source MUST be .js

<script type="text/javascript" src="someJS.php"></script>

There's nothing saying a script source MUST be .js

<script type="text/javascript" src="someJS.php"></script>

Hmm, interesting concept, I'll have to play around with that a little.
Thx.
:-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.