javascript - 可以获取要在 javascript 上设置的变量

标签 javascript variables if-statement livechat

试图使变量 __lc.group 动态,以便根据页面的 URL 设置组号,这是我尝试过的代码,但似乎没有用.

不确定为什么它不起作用。这是为了在网站上动态设置实时聊天功能的组变量,这样我就可以为网站的不同页面设置不同的运算符(operator)。

if(window.location.href.indexOf("netball") > -1) {
       __lc.group = 1;
    }
if(window.location.href.indexOf("football") > -1) {
       __lc.group = 5;
    }
if(window.location.href.indexOf("basketball") > -1) {
       __lc.group = 2;
    }
if(window.location.href.indexOf("social") > -1) {
       __lc.group = 3;
    }
if(window.location.href.indexOf("fitness") > -1) {
       __lc.group = 6;
    }
if(window.location.href.indexOf("softball") > -1) {
       __lc.group = 4;
    }
if(window.location.href.indexOf("volleyball") > -1) {
       __lc.group = 4;
    }
if(window.location.href.indexOf("dodgeball") > -1) {
       __lc.group = 4;
    }
if(window.location.href.indexOf("american") > -1) {
       __lc.group = 4;
    }
if(window.location.href.indexOf("ultimate") > -1) {
       __lc.group = 4;
    }

脚本的完整代码是:

<!-- Start of LiveChat (www.livechatinc.com) code -->
<script type="text/javascript">
var __lc = {};
__lc.license = XXXXX;
if(window.location.href.indexOf("netball") > -1) {
       __lc.group = 1;
    }
if(window.location.href.indexOf("football") > -1) {
       __lc.group = 5;
    }
if(window.location.href.indexOf("basketball") > -1) {
       __lc.group = 2;
    }
if(window.location.href.indexOf("social") > -1) {
       __lc.group = 3;
    }
if(window.location.href.indexOf("fitness") > -1) {
       __lc.group = 6;
    }
if(window.location.href.indexOf("softball") > -1) {
       __lc.group = 4;
    }
if(window.location.href.indexOf("volleyball") > -1) {
       __lc.group = 4;
    }
if(window.location.href.indexOf("dodgeball") > -1) {
       __lc.group = 4;
    }
if(window.location.href.indexOf("american") > -1) {
       __lc.group = 4;
    }
if(window.location.href.indexOf("ultimate") > -1) {
       __lc.group = 4;
    }

(function() {
  var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
  lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.livechatinc.com/tracking.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
})();
</script>
<!-- End of LiveChat code -->

最佳答案

这是一种有效且更优雅的方式来处理检查。它用名称和相应的数字定义一个对象,然后遍历它并与 url 进行比较。 https://jsfiddle.net/gb2tr4a9/

__lc = {license:123456789}
var categories = {
    netball:4 ,
    football:5,
    basketball:2,
    fiddle:99
}
for (var c in categories) {
    if(window.location.href.indexOf(c) != -1) {
       __lc.group = categories[c]; 
       break;
    }
}    
console.log(__lc);     

响应是一个对象,其中 {group=99}

关于javascript - 可以获取要在 javascript 上设置的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32245600/

相关文章:

javascript - 为 web 使用 Windows Aero Window Color

c - C 中的单行 if 语句 - 哪些语句受到影响?

c - 如何使用 sscanf 仅使用 stdio.h 确保输入的字符串长度为 1

javascript - JavaScript( Protractor )中函数的值返回为未定义

javascript - 输入清理 Laravel 4

java - 为什么 boolean 变量不显示 false 的任何结果?

powershell - 传递远程 Powershell 变量

for-loop - 如何使用 awk 和 grep 计算两个文件的平均值

javascript - 自动刷新 <div> 文本

JavaScript 私有(private)变量、函数