如果其他条件重定向

时间:2014-02-17 11:41:12

标签: javascript jquery ajax

我想根据国家/地区重定向

在检测到我要检测的国家/地区后,该用户是来自移动设备还是桌面

然后重定向

jQuery.getJSON('http://freegeoip.net/json/', function(location) {

    if (location.country_code == 'PH' || location.country_code == 'IN') {
        top.location.href = 'xxxxxxxxxxx';
    } else if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {

    } else {
        top.location.href = 'xxxxxxxxxxxxx';
    }
 });

这不起作用

3 个答案:

答案 0 :(得分:1)

jQuery.getJSON('http://freegeoip.net/json/', function (location) {

    if (location.country_code == 'PH' || location.country_code == 'IN') {
        top.location.href = 'xxxxxxxxxxx';
        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {} else {
            top.location.href = 'xxxxxxxxxxxxx';
        }
    } else {
        //code for other countries 
    }
});

答案 1 :(得分:0)

jQuery.getJSON('http://freegeoip.net/json/', function(location) {

if (location.country_code == 'PH' || location.country_code == 'IN') {
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
       top.location.href = 'yyyyyyyyyyyyyy';
    } else {
       top.location.href = 'xxxxxxxxxxxxx';
    }
} else {
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
       top.location.href = 'zzzzzzzzzzzzz';
    } else {
       top.location.href = 'ttttttttttttt';
    }
}
});

答案 2 :(得分:0)

试试这个,

$(function(){
    jQuery.getJSON('http://freegeoip.net/json/', function(location) {

        if (location.country_code == 'PH' || location.country_code == 'IN') { 
                 if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
                     top.location.href = 'xxxxxxxxxxxxx?type=mobile';//mobile
                  } else {
                     top.location.href = 'xxxxxxxxxxxxx?type=desktop';//desktop
                 }
        } else{
              top.location.href = 'xxxxxxxxxxx';// other country
        }
    });
});