views:

323

answers:

8

I can access a PHP var with Javascript like this:

<?php
    $fruit = "apple";
    $color = "red";
?>

<script type="text/javascript">
    alert("fruit: " + "<?php echo $fruit; ?>"); // or shortcut "<?= $fruit ?>"
</script>

But what if I want to use an external JS file:

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

externaljs.js:

alert("color: " + "<?php echo $color; ?>");
A: 

externaljs.js is a static file. Of course it can't access PHP data. The only way to pass PHP data to a js file would be to physically alter the file by writing to it in your PHP script, although this is a messy solution at best.

Edit in response to Ólafur Waage's answer: I guess writing to the js file isn't the only way. Passing the js through the PHP interpreter never crossed my mind (for good reason).

Manos Dilaverakis
+4  A: 

You don't really access it you insert it into the javascript code when you serve the page.

However if your other javascript isn't from an external source you can do something like:

<?php
    $color = "Red";
?>
<script type="text/javascript">var color = "<?= $color ?>";</script>
<script type="text/javascript" src="file.js"></script>

and then in the file.js use color like so:

alert("color: " + color);
Don
That is exactly what I just came up with as well,. and what I was after. +1 for Don
FFish
Should be var color = "<?= $color ?>";
Mark L
Sorry about that and thanks Mark L, I've corrected it in the post.
Don
+3  A: 

What I've seen done is let .js files run through the php interpreter. Which I can not recommend.

What I do recommend is fetching the values through AJAX and have the PHP file return the value to the JS file. Which is a much cleaner method.

Ólafur Waage
A: 

You cant do that and dont try to as this is not a recommended approach, However you can pass php variables as a function parameters to function written in external js

sushil bharwani
+2  A: 

First of all you have to understand that no program can actually have access to the other program's variable.

When you realize that, the rest is simple.
You can either set up a js variable in the main file and then include your external js, or make this external js dynamic, generated by PHP as well

Col. Shrapnel
Thanks for clarifying. I used your first solution. <script type="text/javascript">_fruit = "<?= $fruit; ?>";</script> than <script type="text/javascript" src="externaljs.js"></script> and inside externaljs.js I access with alert(_fruit);
FFish
+1  A: 

What you likely want, is called Asynchronous JavaScript and XML (AJAX): http://www.w3schools.com/ajax/default.aspa

Basically, imagine being able to send messages from the clients JavaScript to your PHP scripts on the server. In the example you gave (externaljs.js), you would have the script ask the server what $color is, via HTTP. You can also point the script tag at a PHP script that generates the JavaScript you want. It depends on what you need to do.

It helps to have some understanding of taint checking, data verification, and security ;)

TerryP
A: 

As the others are saying, javascript doesn't have access to php variables. However, it does have access to the DOM. So, you can use php to add attributes to some page element. And then you can access those attributes with javascript.

e.g. <div id='apple' class='red'> is completely available to javascript

dnagirl
also a good point here girl
FFish
A: 
<script type="text/javascript" src="externaljs.js"></script>

You could change it to

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

And the PHP script could just write JavaScript like that :

<?php
$fruit = "apple";
echo 'var fruit = '.json_encode($fruit);
...

Though using AJAX like said Sepehr Lajevardi would be much cleaner

Serty Oan
I think a header with proper Content-type should be added.
Col. Shrapnel
Classic JavaScript files don't have one, they are plain text files, the tag that reference their URL define their type of use in the browser.
Serty Oan