【问题标题】:Detect Country And Redirect检测国家和重定向
【发布时间】:2014-03-11 20:40:56
【问题描述】:

这是我的代码,但它不起作用。

<!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>

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

【问题讨论】:

    标签: javascript html json jsonp


    【解决方案1】:

    您使用的 wipmania 链接错误,它需要在其中包含可以执行的函数的名称。您可以通过两种方式使用它,一种是通过 jquery 的 jsonp ajax 调用:

    $.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>
    

    【讨论】:

    • 比“不工作”更具描述性,这可能意味着很多事情,您是否遇到错误? jsfiddle.net/LYJtP 这个小提琴表明它在工作
    【解决方案2】:

    您可以通过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

    【讨论】:

    • 我想在html中使用它。我的主机不支持 php 文件。
    • 他已经在使用一个 javascript 库,它的 wipmania 链接他只是使用不正确。
    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    相关资源
    最近更新 更多