javascript - 在javascript变量中添加php定义变量

标签 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

最佳答案

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

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

关于javascript - 在javascript变量中添加php定义变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29167096/

相关文章:

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

PHP:我可以将所需的转义约定传递给 escapeshellarg()

javascript - 无法将带有文本的元素添加到 DOM 树

javascript - 即使在页面刷新react js和react router中也渲染相同的选定数字

javascript - 如何在每次刷新页面时为一行文本生成随机字体?

PHP heredoc 不保留格式

php - Laravel 将 where 子句添加到联合结果

javascript - 可以在页面处理之前修改传入数据的 Firefox 插件?

javascript - 如何检测tinymce 4中菜单何时呈现?

来自数据库的php时间