javascript - Using PHP in .JS file? -


so i'm trying use php in .js file, here's have far.

.htaccess (any *.api.js processed php)

<filesmatch "^.*?api.*?$"> sethandler php54-script </filesmatch> 

map.api.js

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

.php files include

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

for reason isn't working, , after research can't find solution.

in chrome developer tools error, uncaught syntaxerror: unexpected token < - pretty self explanatory, isn't expecting <?php @ top of map.api.js file.

anyone else here tried using php in .js file before? if there better solution i'd know can't find on google.

create file php extension , include in website javascript.

map.api.js

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

in html file:

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

if wan't hide php extension, can work mod_rewrite (apache):

rewriteengine on rewriterule ^map.api.js$ map.api.php 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -