4

So I'm trying to use PHP in a .js file, here's what I have so far.

.htaccess (any *.api.js will be processed as PHP)

<FilesMatch "^.*?api.*?$">
SetHandler php54-script
</FilesMatch>

map.api.js

<?php
header("Content-type: application/javascript");
//my php here
?>

//my javascript here

.php files all include

<script type="text/javascript" src="map.api.js"></script>

For some reason this isn't working, and after much research I can't find a solution.

In the chrome developer tools I get an error, Uncaught SyntaxError: Unexpected token < - pretty self explanatory, it isn't expecting <?php at the top of the map.api.js file.

Anyone else here tried using PHP in a .js file before? If there is a better solution I'd like to know as I can't find much on Google.

3
  • Check this stackoverflow.com/questions/3241422/… Commented Jan 27, 2016 at 13:00
  • I am trying to avoid putting 1000+ lines of javascript at the top of my HTML file, so I'd like to know if there is a solution to the way I'm trying to do this. Commented Jan 27, 2016 at 13:03
  • Your regex seems bad. Try \bapi\b instead of ^.*?api.*?$; Commented Jan 27, 2016 at 13:09

3 Answers 3

7

Create an file with php extension and include it in your website as javascript.

map.api.js

<?php
header("Content-type: application/javascript");
//my php here
?>
//my javascript here

In your HTML File:

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

If you wan't to hide the php extension, you can work with mod_rewrite (Apache):

RewriteEngine on
RewriteRule ^map.api.js$ map.api.php
Sign up to request clarification or add additional context in comments.

2 Comments

For some reason I'm still getting the Uncaught SyntaxError: Unexpected token < error after renaming map.api.js to map.api.php (and updating it in my html/php files)
I removed PHP handlers from the .htaccess file and it now works, thanks!
3

You don't actually need to set PHP as handler for your .api.js files. You can use .php files as scripts too, as long as you preserve your Content-Type header.

So your file would be something like:

<?php
    header("Content-Type: application/javasctipt);
    // code...
?>

alert("This is JS");

And in your HTML page you can include it like this:

<script src="/map.api.php"></script>

This is also useful if you want to manipulate the JS code using PHP before sending it to the client, so you can do something like this:

<script src="/map.api.php?feature=1"></script>

and in your PHP:

<?php
    header("Content-Type: application/javasctipt);
?>

alert("This is JS");

<?php
    if ($_GET["feature"] == "1") {
        echo "alert('Cool feature imported');";
    }
?>

1 Comment

Removed the PHP handlers from .htaccess and used Take's solution. Now it works, thanks Marco!
1

Take's response is the best practice.

If you really want executing PHP code inside dotJS files.

.htaccess

<FilesMatch "\bapi\b">
SetHandler php54-script
</FilesMatch>

And be sure you allow .htaccess in your Apache config:

<Directory /your/path>
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.