2016-12-16 88 views
0

我刚刚发现了根据IP地址重定向访问者的代码。例如,如果我们想将访问者从example.in重定向到example.com,因为“.in”是我们的本地域,“.com”是国际域。自动重定向到特定网站的访问者

我们希望我们的客户只访问来自美国,英国的国际网站,如果他们访问example.in。

问题是,如果他们是从移动设备中挑选出来的,他们没有被重定向。请帮忙。

下面分享我的代码:

<script> 

    jQuery.ajax({ 
    url: '//freegeoip.net/json/', 
    type: 'POST', 
    dataType: 'jsonp', 
    success: function(location) { 
    // If the visitor is browsing from Foreign Country 

    if (location.country_code == 'US' || location.country_code == 'ZA')) { 
     // Redirect to the International store. 
     window.top.location.href = 'http://www.example.com'; 
    } 
    } 
}); 



    </script> 

PS:我把上面的代码中example.in

+0

Wha t是ZA? ZA不是英国,而是南非。检查移动设备是否正在记录。在这个Ajax调用之前并在成功之前放置一个控制台/提醒函数。 – HymnZ

回答

0

试试这个还,但我没试过这个

<script type="application/javascript"> 
$(document).ready(function() { 
$.getJSON("http://www.telize.com/geoip?callback=?", 
function(json) { 
// If the visitor is browsing from China. 
if (json.country_code === 'CN') { 
// Redirect him to the China store. 
window.location.href = 'http://www.mydomainname.cn'; 
} 
} 
); 
}); 
// ]]> 
</script> 
+0

嗨Sankar!感谢您的回复,实际上主要问题在于访问者在使用移动设备或智能手机时不会重定向,否则对于deskti –

0

让我们做这http://jsfiddle.net/kvishnudev/7Ut65/1/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html> 
<head> 
    <title>Get web visitor's location</title> 
    <meta name="robots" value="none" /> 
</head> 
<body> 
<div id="yourinfo"></div> 
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg"></script> 
<script type="text/javascript"> 
var Continent = {"AD":"Europe","AE":"Asia","AF":"Asia","AG":"North America","AI":"North America","AL":"Europe","AM":"Asia","AN":"North America","AO":"Africa","AQ":"Antarctica","AR":"South America","AS":"Australia","AT":"Europe","AU":"Australia","AW":"North America","AZ":"Asia","BA":"Europe","BB":"North America","BD":"Asia","BE":"Europe","BF":"Africa","BG":"Europe","BH":"Asia","BI":"Africa","BJ":"Africa","BM":"North America","BN":"Asia","BO":"South America","BR":"South America","BS":"North America","BT":"Asia","BW":"Africa","BY":"Europe","BZ":"North America","CA":"North America","CC":"Asia","CD":"Africa","CF":"Africa","CG":"Africa","CH":"Europe","CI":"Africa","CK":"Australia","CL":"South America","CM":"Africa","CN":"Asia","CO":"South America","CR":"North America","CU":"North America","CV":"Africa","CX":"Asia","CY":"Asia","CZ":"Europe","DE":"Europe","DJ":"Africa","DK":"Europe","DM":"North America","DO":"North America","DZ":"Africa","EC":"South America","EE":"Europe","EG":"Africa","EH":"Africa","ER":"Africa","ES":"Europe","ET":"Africa","FI":"Europe","FJ":"Australia","FK":"South America","FM":"Australia","FO":"Europe","FR":"Europe","GA":"Africa","GB":"Europe","GD":"North America","GE":"Asia","GF":"South America","GG":"Europe","GH":"Africa","GI":"Europe","GL":"North America","GM":"Africa","GN":"Africa","GP":"North America","GQ":"Africa","GR":"Europe","GS":"Antarctica","GT":"North America","GU":"Australia","GW":"Africa","GY":"South America","HK":"Asia","HN":"North America","HR":"Europe","HT":"North America","HU":"Europe","ID":"Asia","IE":"Europe","IL":"Asia","IM":"Europe","IN":"Asia","IO":"Asia","IQ":"Asia","IR":"Asia","IS":"Europe","IT":"Europe","JE":"Europe","JM":"North America","JO":"Asia","JP":"Asia","KE":"Africa","KG":"Asia","KH":"Asia","KI":"Australia","KM":"Africa","KN":"North America","KP":"Asia","KR":"Asia","KW":"Asia","KY":"North America","KZ":"Asia","LA":"Asia","LB":"Asia","LC":"North America","LI":"Europe","LK":"Asia","LR":"Africa","LS":"Africa","LT":"Europe","LU":"Europe","LV":"Europe","LY":"Africa","MA":"Africa","MC":"Europe","MD":"Europe","ME":"Europe","MG":"Africa","MH":"Australia","MK":"Europe","ML":"Africa","MM":"Asia","MN":"Asia","MO":"Asia","MP":"Australia","MQ":"North America","MR":"Africa","MS":"North America","MT":"Europe","MU":"Africa","MV":"Asia","MW":"Africa","MX":"North America","MY":"Asia","MZ":"Africa","NA":"Africa","NC":"Australia","NE":"Africa","NF":"Australia","NG":"Africa","NI":"North America","NL":"Europe","NO":"Europe","NP":"Asia","NR":"Australia","NU":"Australia","NZ":"Australia","OM":"Asia","PA":"North America","PE":"South America","PF":"Australia","PG":"Australia","PH":"Asia","PK":"Asia","PL":"Europe","PM":"North America","PN":"Australia","PR":"North America","PS":"Asia","PT":"Europe","PW":"Australia","PY":"South America","QA":"Asia","RE":"Africa","RO":"Europe","RS":"Europe","RU":"Europe","RW":"Africa","SA":"Asia","SB":"Australia","SC":"Africa","SD":"Africa","SE":"Europe","SG":"Asia","SH":"Africa","SI":"Europe","SJ":"Europe","SK":"Europe","SL":"Africa","SM":"Europe","SN":"Africa","SO":"Africa","SR":"South America","ST":"Africa","SV":"North America","SY":"Asia","SZ":"Africa","TC":"North America","TD":"Africa","TF":"Antarctica","TG":"Africa","TH":"Asia","TJ":"Asia","TK":"Australia","TM":"Asia","TN":"Africa","TO":"Australia","TR":"Asia","TT":"North America","TV":"Australia","TW":"Asia","TZ":"Africa","UA":"Europe","UG":"Africa","US":"North America","UY":"South America","UZ":"Asia","VC":"North America","VE":"South America","VG":"North America","VI":"North America","VN":"Asia","VU":"Australia","WF":"Australia","WS":"Australia","YE":"Asia","YT":"Africa","ZA":"Africa","ZM":"Africa","ZW":"Africa"}; 
    if(google.loader.ClientLocation) 
    { 
     visitor_lat = google.loader.ClientLocation.latitude; 
     visitor_lon = google.loader.ClientLocation.longitude; 
     visitor_city = google.loader.ClientLocation.address.city; 
     visitor_region = google.loader.ClientLocation.address.region; 
     visitor_country = google.loader.ClientLocation.address.country; 
     visitor_countrycode = google.loader.ClientLocation.address.country_code; 
     if(visitor_countrycode!= null) 
     { 
       var Cont = Continent[visitor_countrycode]; 
       //alert(Cont);    
     } 

     // 
     document.getElementById('yourinfo').innerHTML = '<p>Lat/Lon: ' + visitor_lat + '/' + visitor_lon + '</p><p>Location: ' + visitor_city + ', ' + visitor_region +', Continent : ' + Cont + ', ' + visitor_country + ' (' + visitor_countrycode + ')</p>'; 
    } 
    else 
    { 
     document.getElementById('yourinfo').innerHTML = '<p>Whoops!</p>'; 
    } 
</script> 
</body> 
相关问题