首页 > 解决方案 > 如何通过基于数字的 JavaScript 重定向 URL(如果为 1,则重定向到 A;如果为 2,则重定向到 B...)

问题描述

我想根据 URL 中的变量重定向访问者。用例是重力形式的测验,用户将被重定向到 example.com/processing?quizresult={n}

其中n是一个数值 (0-50)。

有 3 种可能的结果(0-15、16-30、31-50)。

我发现 JavaScript 根据 URL 是否包含变量而不是变量范围来重定向。有比 50 条“IF”语句更好的方法吗?

<script>
if(document.location.href.indexOf('1') > -1) { 
    document.location.href = 'http://www.example.com/resultA';
}

if(document.location.href.indexOf('2') > -1) { 
    document.location.href = 'http://www.example.com/resultA';
}

if(document.location.href.indexOf('3') > -1) { 
    document.location.href = 'http://www.example.com/resultA';
}

标签: javascriptredirectgravity-forms-plugingravityforms

解决方案


嘿,您的解决方案在这里,

尝试这个。

if (document.location.href.indexOf(1) > -1) {
      alert("your url contains the name franky");
    } else if (document.location.href.indexOf(2) > -1) {
      alert("your url contains the name franky");
    } else if (document.location.href.indexOf(3) > -1) {
      alert("your url contains the name franky");
    } else{ alert("not matach any value");}


推荐阅读