我想根据用户所在国家/地区重定向我的网站。例如,如果用户从美国输入www.domain.com,那么他应该重定向到en.domain.com。
答案 0 :(得分:0)
答案 1 :(得分:0)
嗯,为了帮助您入门,以下是使用HTML5地理定位检测某人所在国家/地区的方法:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': <YOURLATLNGRESPONSEFROMGEO>}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var loc = getCountry(results);
}
}
});
function getCountry(results)
{
for (var i = 0; i < results[0].address_components.length; i++)
{
var shortname = results[0].address_components[i].short_name;
var longname = results[0].address_components[i].long_name;
var type = results[0].address_components[i].types;
if (type.indexOf("country") != -1)
{
if (!isNullOrWhitespace(shortname))
{
return shortname;
}
else
{
return longname;
}
}
}
}
function isNullOrWhitespace(text) {
if (text == null) {
return true;
}
return text.replace(/\s/gi, '').length < 1;
}
这将获得该人的国家。不幸的是,我不知道如何使用它来重定向一个人。试试这个链接:http://dev.maxmind.com/geoip/geolite。