【问题标题】:How to redirect URL via JavaScript based on a number (if 1, redirect to A; if 2, to B...)如何通过基于数字的 JavaScript 重定向 URL(如果为 1,则重定向到 A;如果为 2,则重定向到 B...)
【发布时间】:2020-02-15 15:57:42
【问题描述】:

我想根据访问者 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';
}

【问题讨论】:

    标签: javascript redirect gravity-forms-plugin gravityforms


    【解决方案1】:

    试试这个:

    • 从你的 url 字符串中提取 {n}
    • 根据条件有条件地重定向 n的值

    例子:

    // extract {n} from example.com/processing?quizresult={n}
    const getParamValue = (url, parameterKey) => {
      const [urlWithoutParameters, parameters = ''] = url.split('?'); // ['example.com/processing', 'quizresult=1']
      const paramsArr = parameters.split('&'); // ['quizresult=1']
    
      for (let i = 0; i < paramsArr.length; i++) {
        const [k, v] = paramsArr[i].split('='); // k = 'quizresult', v = '1'
        if (k === parameterKey) {
          return v;
        }
      }
    };
    
    // conditionally redirect to the result urls.
    const conditionallyRedirect = (url) => {
      const quizResult = getParamValue(url, 'quizresult');
      if (quizResult >= 0 && quizResult < 16) {
        console.log('resultA');
        // document.location.href = 'http://www.example.com/resultA';
      } else if (quizResult >= 16 && quizResult < 31) {
        console.log('resultB');
        // document.location.href = 'http://www.example.com/resultB';
      } else if (quizResult >= 31 && quizResult <= 50) {
        console.log('resultC');
        // document.location.href = 'http://www.example.com/resultC';
      }
      // do not redirect otherwise
    };
    
    
    // Examples
    conditionallyRedirect('example.com/processing?quizresult=1'); // redirects to resultA
    conditionallyRedirect('example.com/processing?quizresult=17&someotherparam=whatever'); // redirects to resultB
    conditionallyRedirect('example.com/processing'); // does not redirect
    conditionallyRedirect('example.com/processing?quizresult=100'); // does not redirect
    

    所以这对你有用:

    conditionallyRedirect(document.location.href);
    

    【讨论】:

      【解决方案2】:

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

      试试这个。

      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");}

      【讨论】:

      • 如果你想检查 url 是否包含数字,你需要使用不带引号的数字,因为它是一个数字。如果要检查 url 是否包含字符串,则需要使用带引号的数字,因为它是一个字符串。
      猜你喜欢
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      相关资源
      最近更新 更多