通过 SRC 属性将变量传递给 JavaScript [英] Pass vars to JavaScript via the SRC attribute

查看:37
本文介绍了通过 SRC 属性将变量传递给 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 HTML 文件中,我已通过以下方式链接到 JS:

In my HTML file I have linked to the JS with:

src="myscript.js?config=true"

我的JS可以像这样直接读取这个var的值吗?

Can my JS directly read the value of this var like this?

alert (config);

这不起作用,FireFox 错误控制台显示未定义配置".如何读取通过 JS 文件中的 src 属性传递的变量?有这么简单吗?

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>

你不能像你尝试的那样将变量传递给 JS.SCRIPT 标签不会创建 Window 对象(它有一个查询字符串),也不是服务器端代码.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆