-1

所以我在下面使用这个当前脚本:

<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 命令,但似乎无法让它工作,这需要在一个连续的脚本中,有什么想法吗?

4

2 回答 2

1

我制作了这个小脚本以避免有太多的 if / else 语句,基本上你在开始时定义所有检查和重定向,然后让脚本循环遍历它们:

http://jsfiddle.net/XYaFg/1/

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”内容

于 2013-07-08T14:23:06.293 回答
0

请查看W3Schools 网站

if...else if...else 语句

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 语句

于 2013-07-08T14:09:51.213 回答