这个脚本地理定位重定向缺少什么

what's i missing with this script geolocation redirect

我拥有的脚本之一是什么。根据目标国家/地区代码,这不起作用

//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://whosebug.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>

问题是您在 if 块中为各种用户代理初始化了 target。如果用户代理与其中任何一个都不匹配,target 将保持未定义状态,然后 target[g.country_code] 会出错。

你应该在 if 之前将变量初始化为一个空对象,并将默认值 target.All 放在那里。如果你想要根据用户代理的特定位置目标,你可以在 if.

中添加这些目标

另一个问题是响应中没有 country_code 属性。 JSON 看起来像:

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"
});

国家代码在 g.country.code,而不是 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://whosebug.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>