我正在使用Rain.tpl我的网站的模板引擎。我现在有一个变量($username),我可以在常规 html 文件中使用 {$username}
。这很好用。
我现在想在单独的 .js 文件中使用相同的变量,我使用 <script src="/js/MySeparateJsFile.js"></script>
将其加载到 thml 文件中。我尝试使用 {$username}
在其中,但这似乎不起作用。所以我想使用 Javascript 在 html 文件中设置它,但我不知道如何做到这一点。
有人知道如何将 html 文件中的 javascript 变量插入到 .js 文件方法中吗?欢迎所有提示!
最佳答案
在Rain.tpl
文件中将用户名声明为js全局变量,如window.username
。在 MySeparateJsFile.js
文件
Rain.tpl
<script>
window.username = "{$username}";
</script>
js文件
var username = window.username;
关于javascript - 如何使用 javascript 将变量从 html 文件插入到 js 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23607596/