-2

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>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and edit your question accordingly. – coderpc Jul 03 '18 at 20:11
  • Just fyi, you are creating an array but `target.US` and the other assignments are not how you add array elements. I think you just want a plain object, `target={}` – Patrick Evans Jul 03 '18 at 20:13

1 Answers1

0

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>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I already gave it. the real problem is the script is not able to work on the ip was just headed to target all. how people can be diverted automatically to the geo that I pairs such as the us – Heri purwanto Jul 04 '18 at 11:47
  • There's no `country_code` property in the response. It should be `g.country.code`. – Barmar Jul 04 '18 at 21:32