2014-02-13 123 views
2

这是我的代码,但它不起作用。检测国家和重定向

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
<script src="http://api.wipmania.com/jsonp?callback.js"></script> 
<script type="text/javascript"> 
switch(country_code()){ 
case "IN" : 
     document.location.href = "http://xxxxxxxx.biz/theme.php"; 
     break; 
} 
</script> 
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
</html> 

我想检测用户的国家,并适当地重定向他们。

回答

5

您正在使用wipmania链接错误,它需要有它的功能是可以执行的名称。您可以通过以下两种方式之一通过JSONP AJAX调用使用jQuery使用它:

$.ajax({ 
    url:"http://api.wipmania.com/jsonp?callback=?", 
    dataType:"jsonp" 
}).done(function(data){ 
    switch(data.address.country_code){ 
     case "IN" : 
      document.location.href = "http://xxxxxxxx.biz/theme.php"; 
     break; 
    }  
}); 

或在你的代码中创建一个函数,然后在wipmania链接

<script> 
function determineCountry(data){ 
    switch(data.address.country_code){ 
     case "IN" : 
     document.location.href = "http://xxxxxxxx.biz/theme.php"; 
     break; 
    }  
} 
</script> 
<script type="text/javascript" src="http://api.wipmania.com/jsonp?callback=determineCountry"></script> 
+0

无法正常工作。请任何例子。 – user3304678

+0

比可能意味着很多事情的“不工作”更具描述性,您是否收到错误? http://jsfiddle.net/LYJtP/这个小提琴显示它的工作 –

+0

现在工作:)谢谢 – user3304678

0

您可以通过php来实现。这应该让你在那里。

<?php 
// ccr.php - country code redirect 
require_once('geoplugin.class.php'); 
$geoplugin = new geoPlugin(); 
$geoplugin->locate(); 
$country_code = $geoplugin->countryCode; 
switch($country_code) { 
case 'US': 
header('Location: http://www.domain.com/links/usa.php'); 
exit; 
case 'CA': 
header('Location: http://www.domain.com/links/canada.php'); 
exit; 
case 'GB': 
header('Location: http://www.domain.com/links/greatbritain.php'); 
exit; 
case 'IE': 
header('Location: http://www.domain.com/links/ireland.php'); 
exit; 
case 'AU': 
header('Location: http://www.domain.com/links/australia.php'); 
exit; 
case 'NZ': 
header('Location: http://www.domain.com/links/newzealand.php'); 
exit; 
default: // exceptions 
header('Location: http://www.domain.com/links/allelse.php'); 
exit; 
} 
?> 

更多的信息和支持插件下载:

http://www.trafficplusconversion.com/251/redirect-based-on-country/

这里是一个JavaScript选项。

http://www.geobytes.com/GeoDirection.htm

+0

我想使用该功能的名称在html中使用它。我的主机不支持PHP文件。 – user3304678

+0

他已经在使用JavaScript库,它的wipmania链接,他只是使用不正确。 –