Access PHP var from external javascript file

Posted by FFish on Stack Overflow See other posts from Stack Overflow or by FFish
Published on 2010-05-28T12:13:34Z Indexed on 2010/05/28 12:22 UTC
Read the original article Hit count: 169

Filed under:
|
|
|

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; ?>");

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript