在javascript变量中添加php define变量

时间:2015-03-20 12:59:44

标签: javascript php

我有一个php变量,我需要传递到javascript函数中的链接

//In the config
<?php
    define('THE_VAR', 'test');
?>


//On the page
<?php
    $the_var = THE_VAR;
?>

<script>
    (function()){
        link = "test.site"+<?php echo json_encode($the_var); ?>+"/testing.js"
    })();
</script>

我需要javascript函数中的链接来阅读。

test.site/test/testing.js

2 个答案:

答案 0 :(得分:2)

而不是:link = "test.site"+<?php echo json_encode($the_var); ?>+"/testing.js"

使用:link = "test.site/<?= $the_var; ?>/testing.js";

答案 1 :(得分:-1)

使用Ajax将数据从php传递到javascript。

http://www.w3schools.com/ajax/ajax_php.asp