javascript 文件中的 php 常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6401297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php constants in a javascript file
提问by Mac Taylor
Possible Duplicate:
Pass a PHP string to a Javascript variable (and escape newlines)
I'm working on a project and it's already done, but it needs one more midification and its applying language constants in javascript files.
我正在做一个项目,它已经完成了,但它需要更多的中间化和它在 javascript 文件中的应用语言常量。
consider we include a javascript file and in that we have something like this
考虑我们包含一个 javascript 文件,我们有这样的东西
alert("YOU Have VOTED BEFORE");
easy like that , the script will alert that text
就这么简单,脚本会提醒那个文本
but what if we need to alert a language constant of it :
但是如果我们需要提醒一个语言常量呢:
alert("<?php echo _VOTED_BEFORE?>");
this will be worked only if echo the script like inline of my php codes ...
只有在像我的 php 代码的内联一样回显脚本时,这才有效......
but, how can we read and include php constants or $vars for a outside JavaScript file ???
但是,我们如何读取和包含外部 JavaScript 文件的 php 常量或 $vars ???
采纳答案by Ibu
There is a way of doing this using a query string in the script path
有一种方法可以使用脚本路径中的查询字符串来执行此操作
See my answer here
在这里看到我的答案
How to pass variable from php template to javascript
Unfortunately this will break the cache for your js file so weight your options first
不幸的是,这会破坏您的 js 文件的缓存,因此请先权衡您的选择
<script type="text/javascript" src="script.js?flag=<?php echo _VOTED_BEFORE?>"></script>
for the sake of not writing too much code refer to the link to see how to retrieve the value
为了不写太多代码,请参阅链接以查看如何检索值
回答by Felix Kling
For a cleaner structure, I think the best way is to set all the data you get from PHP in one place, i.e. in the HTML you serve via PHP:
为了更清晰的结构,我认为最好的方法是将您从 PHP 获得的所有数据都设置在一个地方,即在您通过 PHP 提供的 HTML 中:
<script>
var MyNameSpace = {
config:
something: "<?php echo _VOTED_BEFORE ?>"
}
}
</script>
In the JavaScript file you include afterwards, you can access the value via MyNameSpace.config.something
.
在之后包含的 JavaScript 文件中,您可以通过MyNameSpace.config.something
.
This makes it also easier to reuse the message.
这也使得重用消息变得更加容易。
回答by Jivings
If it's not a PHP file, you cannot include PHP echo functions in it. I suggest that if you want to use a PHP variable in your external js files then declare it as a global in your PHP file before you reference the external js.
如果它不是 PHP 文件,则不能在其中包含 PHP 回显函数。我建议,如果您想在外部 js 文件中使用 PHP 变量,请在引用外部 js 之前在 PHP 文件中将其声明为全局变量。
<script type="text/javascript">
var globalvar = '<?php echo _VOTED_BEFORE ?>' ;
</script>
<script type="text/javascript" src="externalfile.js"></script>
Though it's not always a good idea to clutter the global namespace.
尽管弄乱全局命名空间并不总是一个好主意。
回答by stefgosselin
Actually, what you had was close to being proper.
事实上,你所拥有的已经接近于正确的了。
<?php
// Valid constant names
define("VOTED_BEFORE", "false");
?>
<script type="text/javascript">
alert("<?php echo VOTED_BEFORE;?>");
</script>
回答by Serj Sagan
You can use cookies to save these constants and read them in via JavaScript or you will have to use AJAX
您可以使用 cookie 来保存这些常量并通过 JavaScript 读取它们,否则您将不得不使用 AJAX