Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据geoip国家/地区使用JavaScript更改URL_Javascript_Geoip_Geotargetting - Fatal编程技术网

根据geoip国家/地区使用JavaScript更改URL

根据geoip国家/地区使用JavaScript更改URL,javascript,geoip,geotargetting,Javascript,Geoip,Geotargetting,我正在寻找一种方法来更改基于国家的锚定标记的url。我正在使用maxmind的Geoip2JavaScript服务来检测这个国家 我想在单击时更改URL,所以我没有给maxmind打不必要的电话。这是个好主意还是有更好的方法 下面是到目前为止我得到的代码,但它不起作用: var theAnchor = document.getElementsByClassName('geotarget'); theAnchor.onclick = function () { var country = g

我正在寻找一种方法来更改基于国家的锚定标记的url。我正在使用maxmind的Geoip2JavaScript服务来检测这个国家

我想在单击时更改URL,所以我没有给maxmind打不必要的电话。这是个好主意还是有更好的方法

下面是到目前为止我得到的代码,但它不起作用:

var theAnchor = document.getElementsByClassName('geotarget');

theAnchor.onclick = function () {
  var country = geoipResponse.country.iso_code;
  var linkUK = "<?php the_field('link_uk'); ?>";
  var linknUS = "<?php the_field('link_us'); ?>";
  if ( country == "GB" ){
    window.open(linkUK);
  } else {
   window.open(linkUS);
  }
};
HTML:


我会在页面加载时替换URL,这样人们仍然可以单击鼠标中键在新选项卡中打开。链接上的点击事件总是困扰着我

由于要减少对geoip2服务的调用量,您可以点击它一次,然后将结果缓存到服务器上的会话中

大概是这样的:

$(function () {
  var country = "<?php COUNTRY_CODE ?>";
  if(!country)
  {
    country = geoipResponse.country.iso_code;
  }

  var linkUK = "<?php the_field('link_uk'); ?>";
  var linknUS = "<?php the_field('link_us'); ?>";
  if ( country == "GB" ){
    $('.geotarget').attr("href", linkUK)
  } else {
    $('.geotarget').attr("href", linkUS)
  }
});
$(function () {
  var country = "<?php COUNTRY_CODE ?>";
  if(!country)
  {
    country = geoipResponse.country.iso_code;
  }

  var linkUK = "<?php the_field('link_uk'); ?>";
  var linknUS = "<?php the_field('link_us'); ?>";
  if ( country == "GB" ){
    $('.geotarget').attr("href", linkUK)
  } else {
    $('.geotarget').attr("href", linkUS)
  }
});