执行if,如果window.location包含两个字符串和一个指定的变量
我正在使用这个 javascript,
<script>
var windowloc = window.location;
var pink ="http://my.blogspot.com/search/label/Pink%20Template";
var blue ="http://my.blogspot.com/search/label/blue%20Template";
var yellow ="http://my.blogspot.com/search/label/yellow%20Template";
if((windowloc==pink)||(windowloc==blue)||(windowloc==yellow)){
//execute JavaScript
}
</script>
还有更多的变量
,如果 JS if
是类似于 "+colorss+"
的内容,我会更喜欢它> 可以是指定可能性的数组,我已经尝试过以下 编辑它必须包含http://my.blogspot.com/search/label/
然后是指定的变量,然后是%20Template
。
<script>
var windowloc = window.location;
var colorss = "'Pink'||'yellow'||'blue'";
if(windowloc=='http://my.blogspot.com/search/label/'+colorss+'%20Template'){
//execute JavaScript
}
</script>
I am using this javascript
<script>
var windowloc = window.location;
var pink ="http://my.blogspot.com/search/label/Pink%20Template";
var blue ="http://my.blogspot.com/search/label/blue%20Template";
var yellow ="http://my.blogspot.com/search/label/yellow%20Template";
if((windowloc==pink)||(windowloc==blue)||(windowloc==yellow)){
//execute JavaScript
}
</script>
there are many more variables
and I would much prefer it if the JS if
was something along the lines where "+colorss+"
can be an array of specified possibilities, I have tried the below
EDIT it must contain http://my.blogspot.com/search/label/
THEN a specified variable and then %20Template
.
<script>
var windowloc = window.location;
var colorss = "'Pink'||'yellow'||'blue'";
if(windowloc=='http://my.blogspot.com/search/label/'+colorss+'%20Template'){
//execute JavaScript
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 regexp 来完成此操作:
或者如果您想匹配整个 URL:
You can do this using regexp:
Or if you want to match the whole URL:
尽管使用正则表达式可以很好地工作,但这是一种替代方案:
简单的循环就是此类事情所必需的。
Although doing it with regular expressions would work fine, this is an alternative:
Simple loops are all that is neccessary for this sort of thing.
假设,您想要为一组没有模式的 URL 或多个具有特定模式(例如 100 种不同颜色)的可管理 URL 提供特定代码。
页面中有一个元素'spl_code'字段并在JS层编写以下代码
suppose, you want to have specific code for set of URLs which doesn't have a pattern or more than manageable URLs with specific pattern(like 100 different colors).
Have a element 'spl_code' field in a page and write following code in JS layer