通过SRC属性将变量传递给JavaScript [英] Pass vars to JavaScript via the SRC attribute
本文介绍了通过SRC属性将变量传递给JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
src =myscript.js?config = true
我的JS可以直接读取这个变量的值吗?
alert(config);
这不起作用,并且FireFox错误控制台显示配置未定义。如何读取通过JS文件中的src属性传递的变量?这是简单的吗?
解决方案
< script>
var config = true;
< / script>
< script src =myscript.js>< / script>
您无法按照您尝试的方式将变量传递给JS。 SCRIPT标签不会创建一个Window对象(它有一个查询字符串),它不是服务器端代码。
In my HTML file I have linked to the JS with:
src="myscript.js?config=true"
Can my JS directly read the value of this var like this?
alert (config);
This does not work, and the FireFox Error Console says "config is not defined". How do I read the vars passed via the src attribute in the JS file? Is it this simple?
解决方案
<script>
var config=true;
</script>
<script src="myscript.js"></script>
You can't pass variables to JS the way you tried. SCRIPT tag does not create a Window object (which has a query string), and it is not server side code.
这篇关于通过SRC属性将变量传递给JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文