将Querystring样式参数传递到Javascript文件中 [英] Passing Querystring style parameters into Javascript file

查看:58
本文介绍了将Querystring样式参数传递到Javascript文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定这是否可行,或者即使我应该这样做,但我认为这很有趣。

Not sure if this is possible or even if I should do it, but I think it's quite interesting.

我有一个我正在引用的javascript文件一个平面的HTML页面。我想通过脚本的路径传入一个或两个参数。像这样;

I have a javascript file which I'm referencing in a flat HTML page. I'd like to pass in a parameter or two via the path to the script. Like this;

<script src="/scripts/myJavascriptFile.js?config1=true" type="text/javascript"></script>

不确定它是否可行但是这会让我的解决方案更容易让其他人接受我的脚本和工具(可论证)。

Not really sure if it can work but it would make my solution a little easier for others to take my script and implement (arguable).

干杯,
迈克

推荐答案

我不认为通过 src 属性传递变量是可能的,而且没有一些额外的编码(有一篇文章这里 )。您可以执行以下操作,它应提供与您要查找的功能相同的功能:

I don't think that passing in variables via the src attribute is possible out of the box without some extra coding on your part (there is an article here if you are interested!). You could do the following though, which should provide the same functionality as you are looking for:

在HTML页面的单个脚本块中定义config变量:

Define your "config" variables in a single script block on your HTML page:

<script type="text/javascript">
  var config1 = true;
</script>

在第二个脚本块中引用外部JS文件:

Reference your external JS file in a second script block:

<script src="/scripts/myJavascriptFile.js" type="text/javascript"></script>

将此代码添加到外部JS文件中以引用HTML中的local变量:

Add this code to your external JS file to reference the "local" variable in your HTML:

var conf1 = window.config1;

if (conf1) {
  // Do stuff
}

这篇关于将Querystring样式参数传递到Javascript文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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