所以我正在使用下面的当前脚本:
<script type="text/javascript">
if (window.location.href.indexOf('thevault') > -1) {
//window.location = 'http://thevault.co.uk/index.phtml?d=512060’;
window.location = '/index.phtml?d=512060';
} else {
//window.location = 'http://www.jths.co.uk/index.phtml?d=541300';
window.location = '/index.phtml?d=541300';
}
</script>
哪个工作正常,但我想添加另一个条件
如果网址为:http://www.nationalforestteachingschool.co.uk,请转到此页:
https://thevault.jths.co.uk/index.phtml?d=550968
我已经尝试了else if命令但似乎无法使其工作,这需要在一个连续的脚本中,任何想法?
答案 0 :(得分:1)
我制作了这个小脚本以避免使用过多的if / else语句,基本上你在开头定义了所有的检查和重定向,然后让脚本循环遍历它们:
var Redirects = function(currentUrl) {
var urls = [
{
check : 'thevault',
redirect : 'http://thevault.co.uk/index.phtml?d=512060'
},
{
check : 'fiddle',
redirect : 'http://another-url-to-redirect-to.html'
},
{
check : 'last-url-check',
redirect : 'http://some-more-redirect.html'
},
];
this.init = function() {
var length = urls.length;
for ( var i = 0 ; i < length ; i++) {
var current = urls[i];
if( currentUrl.indexOf(current.check) > -1) {
alert("Redirecting to " + current.redirect);
//window.location = current.redirect
}
}
}
this.init();
}
var redirects = new Redirects(window.location.href)
您必须取消注释“window.location”行并注释掉“alert”内容
答案 1 :(得分:0)
请查看W3Schools site。
if ... else if ...... else Statement
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if neither condition1 nor condition2 is true
}
如果需要,您可以链接许多else if
块。
考虑使用Switch statement。