否则,如果,URL重定向 [英] Else if, URL redirection

查看:170
本文介绍了否则,如果,URL重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用下面的当前脚本:

So I am using this current script below:

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

哪个工作正常,但我想添加另一个条件

Which works fine, however I want to add another condition

如果网址是: http://www.nationalforestteachingschool.co.uk ,那么转到此页:

if the url is: http://www.nationalforestteachingschool.co.uk , then go to this page:

https://thevault.jths.co.uk/index.phtml?d=550968

我已尝试过else if命令但不能似乎让它工作,这需要在一个连续的脚本,任何想法??

I have tried the else if command but can't seem to get it working, this needs to be in one continous script, any ideas??

推荐答案

我把这个小脚本改为避免使用过多的if / else语句,基本上你在开头定义所有的检查和重定向,然后让脚本循环遍历它们:

I made this small script to avoid having too many if / else statements, basically you define all your checks and redirects in the beginning and then let the script loop through them:

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

You have to uncomment the "window.location" row and comment out the "alert" stuff

这篇关于否则,如果,URL重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆