Search code examples
javascripthtmlgeolocationgeo

what's i missing with this script geolocation redirect


what was one of the scripts that I have. this does not work according to the target country code

//OFFER WAP
if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone/i) ||
  navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) ||
  navigator.userAgent.match(/Windows Phone/i) || navigator.userAgent.match(/iPad/i)) {
  var target = []; // 
  target.US = "https://www.google.com"; // 
  target.AU = "https://stackoverflow.com/"; // 
  target.All = "https://www.facebook.com/"; // 
  setTimeout("document.location = urls;", 1500);
}

function geoip(g) {
  window.top.location.href = target[g.country_code] || target.All
}
(function(g, e, o, i, p) {
  i = g.createElement(e), p = g.getElementsByTagName(e)[0];
  i.async = 0;
  i.src = o;
  p.parentNode.insertBefore(i, p)
})(document, 'script', 'http://geoip.nekudo.com/api/?callback=geoip');
<meta charset="utf-8">
<title>Please Wait...</title>
<meta http-equiv="refresh" content="1500">
<script src='http://www.geoplugin.net/javascript.gp' type='text/javascript'></script>


Solution

  • The problem is that you have the initialization of target inside the if block for various user agents. If the user agent doesn't match any of them, target is left undefined, and then target[g.country_code] gets an error.

    You should initialize the variable to an empty object before the if, and also put the default target.All there. If you want location-specific targets depending on the user agent, you can add those inside the if.

    Another problem is that there's no country_code property in the response. The JSON looks like:

    geoip({
      "city": "Woburn",
      "country": {
        "name": "United States",
        "code": "US"
      },
      "location": {
        "accuracy_radius": 5,
        "latitude": 42.4897,
        "longitude": -71.1595,
        "time_zone": "America/New_York"
      },
      "ip": "71.192.114.133"
    });
    

    The country code is in g.country.code, not g.country_code.

    var target = { All: "https://www.facebook.com/" };
    
    //OFFER WAP
    if (navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone/i) ||
      navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/BlackBerry/i) ||
      navigator.userAgent.match(/Windows Phone/i) || navigator.userAgent.match(/iPad/i)) {
      target.US = "https://www.google.com"; // 
      target.AU = "https://stackoverflow.com/"; // 
      setTimeout("document.location = urls;", 1500);
    }
    
    function geoip(g) {
      window.top.location.href = target[g.country.code] || target.All
    }
    (function(g, e, o, i, p) {
      i = g.createElement(e), p = g.getElementsByTagName(e)[0];
      i.async = 0;
      i.src = o;
      p.parentNode.insertBefore(i, p)
    })(document, 'script', 'http://geoip.nekudo.com/api/?callback=geoip');
    <meta charset="utf-8">
    <title>Please Wait...</title>
    <meta http-equiv="refresh" content="1500">
    <script src='http://www.geoplugin.net/javascript.gp' type='text/javascript'></script>