Php parse inside javascript (.js)

Hello everybody.

First of all - I have to mention that this is NOT a duplicate question.
I have been passing through every site and questions alike this one. Still no answer had worked for me.

I’m using wamp server, apache and MySQL.

I want to use “cross embedding” which means I want to use a PHP CODE inside a JAVASCRIPT file with a .js ending to it.

I also want to do it “discreetly” which means I want to use JAVASCRIPT and to parse it with PHP in a way people won’t be able to know I that I’m using PHP.

I have tried to position .htaccess file in my root directory and still I can’t parse PHP inside JAVASCRIPT file.

Why doesn’t it work?
How can I fix it?

Need your help!

Did you tried Apache handlers?
Apache handlers control how the Apache web server software manages certain file types and extensions for your site.
For example, to have the server treat files with the extension .js as PHP files, you would type “php5-script” under Handler and “.js” under Extension(s).
AddHandler application/x-httpd-php .php
or I think:
AddHandler php5-script .php

PHP is a server side language so you can’t parse it using JavaScript as it executes within browsers only, if you need to include some kind of configuration in your JavaScript code you will need to do one of the following.

  1. Use a .htaccess rewrite rule so it calls a PHP file that outputs a header of text/javascript
  2. Create an object within a <script> tag that you output values to and call in your .js file

Personally I would choose option 2 as it’s easier to work with and is faster for execution times as it doesn’t require an extra header to be called, see the example below.

	var opts = {
		option_one = <?php echo "'{$some_variable}'"; ?>;

The header and the <script> tags could be omitted if the right actions are performed.

The .htaccess file should solve it all.

It might be that my problem is the .htaccess file.

I’ve put in in root folder and written what’s needed in it. Is there anything more to do ?

Hi questionnaire, welcome to the forums.

There is more than one way to approach this.

The easiest and usual way is to have the PHP code output the JavaScript code into the page itself.

If you want to have a PHP generated JavaScript file AFAIK there are two options.

[list][]Modify your Apache settings to have the PHP engine process files ending with the “.js” extension (IMHO not a good idea)
]Use htaccess to rewrite a request for the JavaScript file to go to the PHP file[/list]

The last seems to be what yo’re trying to do.

Please post the htaccess rules and the PHP code you are trying.

To create an external JavaScript file that needs to be parsed first for PHP:

  1. Specify the correct MIME type in the script tag and use a PHP extension for the file
<script type="application/javascript" src="myjavascript.php"></script>

(substitute the long deprecated “text/javascript” MIME type if you also need the script to work as JScript in EI8 and earlier).

  1. Repeat the MIME type in the file header just in case the browser ignores the one in the HTML and then output the JavaScript
header('Content-type: application/javascript');
$some_variable = 'whatever';
var opts = {
		option_one = <?php echo "'{$some_variable}'"; ?>;