【问题标题】:Can get variable to set on javascript可以在javascript上设置变量
【发布时间】:2015-08-27 09:30:19
【问题描述】:

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

不知道为什么它不起作用。这是为现场实时聊天功能动态设置组变量,以便我可以为站点的不同页面设置不同的运算符。

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

【问题讨论】:

  • “但它似乎没有用”到底是什么意思?你有任何错误吗? __lc变量在使用前你定义了吗?
  • 对不起,我已经添加了完整的代码。目前它没有改变组变量:livechatinc.com/kb/livechat-for-multiple-websites

标签: javascript variables if-statement livechat


【解决方案1】:

这是一种处理检查的有效且更优雅的方式。它使用名称和相应的数字定义一个对象,然后对其进行迭代并与 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}

【讨论】:

  • +1 虽然找到匹配时的break 将是画龙点睛(性能方面,但也以防万一有顺序,例如 left-foot-football:6 football:5总是会被后者覆盖)
  • 非常感谢这种更优雅的做事方式,我仍在努力学习基础知识,我仍然在努力将其用于实时聊天提供的原始代码(在原帖)。我是否将其插入 lc.liscence 下并且不会影响该变量? livechatinc.com/kb/livechat-for-multiple-websites
  • 另外,当我去那个 JSfiddle 并点击运行它实际上并没有在 console.log 中为我显示任何东西
  • 正确,只要在 __lc = {} 之后添加内容,对象的不同属性就会被存储。您可以将该对象声明为我的更新答案,但奇怪的是您没有从示例中获得任何输出,它的 vanilla JS 应该可以在任何地方工作。我正在运行 Firefox 40.0.2
猜你喜欢
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-29
  • 2019-11-19
  • 1970-01-01
相关资源
最近更新 更多