javascript - 如何通过基于数字的 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';
}
解决方案
嘿,您的解决方案在这里,
尝试这个。
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");}
推荐阅读
- r - 创建一个新的数据框列,该列返回另一列元素在给定列表中出现的位置
- android - Android base64 PDF打印选项
- entity-framework - 如何有效地遍历所有行并在 EF Core 中应用更新?
- javascript - Ajax 调用不等待变量刷新
- angular - 为什么迭代 ViewChildren QueryList 只显示一个数组元素?
- amazon-web-services - 如何使用 CloudFormation 为多个日志组创建 AWS CloudWatch 订阅过滤器
- php - 如何在 MYSQL Point 中存储两个 API 值
- python - 如何重新排序一个 numpy 数组(矩阵),使得 Matrix = [ Identity | Residue ](只是移动列)
- javascript - 我怎样才能第二次使用停止?
- android - 无法在 Kotlin 的电子邮件编写器中获取“粗体”HTML 文本