2014-02-17 58 views
0

我想根據國家的if else條件重定向

檢測國家,我想檢測後重定向是移動用戶或桌面

,然後重定向

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'; 
    } 
}); 

這不是工作

+0

'如果/ ELSEIF/else'聲明將讓你的代碼運行只能通過一個人,不是所有的人。 – BeNdErR

回答

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 
     } 
    }); 
}); 
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 
    } 
}); 
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'; 
    } 
} 
});