Static files of JavaScript would survive most applications but sometimes the ability to include PHP code inside JavaScript scripts and generate the content of the script files on the fly by PHP is a better option. How does one do that?
The simplest solution is to just include PHP code inside the <script></script> section of your HTML templates / web pages because chances are, the file extension is .php:
<script>
var jsVar = "<?php echo $phpVar ?>";
</script>
Even if it’s not ending in .php, such as in .html or .htm, you can configure your server to parse all .html or .htm files for any PHP code, though with a little extra server burden.
Another solution for this is to make your server parse all files ending in .js. Just create a .htaccess if it doesn’t exist in the directory in which you wish to include and run PHP code inside all .js files. Add these lines at the end of .htaccess:
AddType application/x-httpd-php .js
AddHandler x-httpd-php5 .js
<FilesMatch "\.(js|php)$">
SetHandler application/x-httpd-php
</FilesMatch>
Now you can add all the PHP code you can inside any JavaScript files with a .js extension, the server will automatically parse and run them when the client side requests the file.
15 replies on “How to execute / run PHP code inside JavaScript files?”
[…] embed PHP code in *.js files you have to first make the server to parse JavaScript .js files for any PHP code so that they will be executed. After that, you can create classes or call to functions or do […]
You could also create a file with a .php extension that returns all the proper headers, and then alias it with mod_rewrite.
This is an alternative to configuring your server to parse .js files if you don’t have the ability to do this.
the best method good bro
[…] Source: https://www.kavoir.com/2010/07/how-to-execute-run-php-code-inside-javascript-files.html […]
[…] https://www.kavoir.com/2010/07/how-to-execute-run-php-code-inside-javascript-files.html Rate this: Share this:StumbleUponDiggRedditFacebookTwitterLike this:LikeBe the first to like this […]
[…] https://www.kavoir.com/2010/07/how-to-execute-run-php-code-inside-javascript-files.html Posted in Uncategorized You can leave a response, or trackback from your own […]
[…] kavoir.com […]
[…] 11. Chạy PHP trong Javascript Khi code JavaScript, nó có thể rất hữu ích để có thể sá» dụng PHP bên trong các táºp tin js, ví dụ như để lấy dữ liệu từ cÆ¡ sở dữ liệu cá»§a bạn. Dưới Ä‘ây là má»™t Ä‘oạn để cho phép việc sá» dụng PHP trong file js. AddType application/x-httpd-php .js AddHandler x-httpd-php5 .js <FilesMatch ".(js|php)$"> SetHandler application/x-httpd-php </FilesMatch> Nguồn: https://www.kavoir.com/2010/07/how-to-execute-run-php-code-inside-javascript-files.html […]
[…] 7. Run PHP Code Inside Javascript […]
[…] : https://www.kavoir.com/2010/07/how-to-execute-run-php-code-inside-javascript-files.html 0.000000 0.000000 Share […]
I’ve got an easy way without disturbing the .htaccess file. Is is very easy, just see how this works…
//Here’s how to call a php file as javascript file
//And this is the code of some_file.php file
[…] Source: https://www.kavoir.com/2010/07/how-to-execute-run-php-code-inside-javascript-files.html […]
Hey, thanks a lot. This saved me a lot of time in my project. Great job!
Thanks! Worked in codeigniter too 🙂
[…] SetHandler application/x-httpd-php </FilesMatch>I found this useful code from https://www.kavoir.com/2010/07/how-to-execute-run-php-code-inside-javascript-files.htmlVery useful Share this:TwitterFacebookLike this:LikeBe the first to like this. Category : […]