Search code examples
javascripthtmljsonjsonp

Detect Country And Redirect


Here is my code, but it's not working.

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

I want to detect the user's country and redirect them appropriately.


Solution

  • You are using the wipmania link wrong it needs to have the name of a function in it that can be executed. You can use it in two ways one through an jsonp ajax call with 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;
         }      
    });
    

    or create a function in your code and then use the name of that function in the wipmania link

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