2014-02-13 69 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鏈接,他只是使用不正確。 –