开发者

Passing PHP variables to non-native JavaScript

Basically, the problem is this:

<?php $test="foobar"; ?>

If within the html document I call

<script type="text/javascript">alert("<?php echo $test; ?>")</script>

, everything is fine.

However, if I do the same thing in an external jS document included with

<script 开发者_JAVA技巧type="text/javascript" src="foo.js"></script> 

it does not work.

Is there any way to do this?


In the first case you actually pass the value to the PHP script, where <?php echo $test; ?> is parsed by PHP parser and replaced by the value of $test PHP variable.

In the second case you just include '<?php echo $test; ?>' string into JS file and this is not parsed.

My suggestion is: pass this value the way you are passing now, but properly reference it from the external JS file. For example you can invoke some function defined in JS file, passing the parameter from PHP file.

There is also the ugly way of doing that, such as making your server treat JS files as PHP files, but I am not recommending it.

Some example:

If you include in your PHP file:

<script type="text/javascript" src="foo.js"></script>
<script type="text/javascript">myHappyFunction("<?php echo $test; ?>")</script>

and in your JS file (foo.js):

(function(){
    var myHappyFunction = function(myParameter){
        alert(myParameter);
    };
    window['myHappyFunction'] = myHappyFunction; // pass outside scope
})();

you should see $test value being alerted when the script executes (assuming I did not make a mistake writing this ;)).


You can try something like this:

http://www.javascriptkit.com/javatutors/externalphp.shtml

The external file doesn't have to be a .js extension. Alternatively, you could associate .js files as PHP files in apache (or whatever webserver you are running). Saving your JS files as .php is probably easiest but keep in mind that it won't share the same variables as the script rendering the page.


If you want to process PHP within JavaScript files, you have a few options. In the typical Apache setup, PHP files with extensions like .php .php3 .php4 .php5 are specifically defined within a configuration file, allowing them to be parsed by PHP. Apache, by default, will not treat JavaScript and other files (like images) as if they were PHP files. If Apache did that, performance would be degraded. So here's what we can do...


Option A

Option A treats all JavaScript files as PHP files, so any PHP will be processed, while outputting the JavaScript to the browser. You'll want to add a header() call at the beginning to ensure the browser knows its a JavaScript file.

httpd.conf

AddType application/x-httpd-php .js


Option B

Option B allows you to add code to your Apache configuration file(s) to route all JS requests to a PHP file for processing. In this option, the scripts.php file would process the $_REQUEST['src'] variable, and grab the appropriate JS file.

.htaccess (not tested, but something like this should work):

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(*\.js)$ scripts.php?src=$1 [L,QSA]
</IfModule>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜