通过JS重定向取决于location.href

时间:2018-04-13 14:40:25

标签: javascript jquery url-redirection

我尝试做的是,如果用户访问网址中包含特定字符串的网站,则应将其随机重定向到网站A或网站B.

这是我的代码:

  if(window.location.href.indexOf('specialstring1') > -1) {
    if(Math.random() > 0.5) {
      window.location.href = "/versionA" + document.location.search;
      } else {
      window.location.href = "/versionB" + document.location.search;
    }
  }
  else if(window.location.href.indexOf('specialstring2') > -1) {
    if(Math.random() > 0.5) {
      window.location.href = "/versionC" + document.location.search;
      } else {
      window.location.href = "/versionD" + document.location.search;
    }
  }

通过location.href重定向取决于字符串是完全独立的工作。与A / B开关相同。似乎,我把它们组合在一起是错误的。

0 个答案:

没有答案
相关问题