专研WordPress,外贸B端网站建设及引流

js 获取浏览器ip、城市并进行跳转

请求网址:

https://pv.sohu.com/cityjson?ie=utf-8

返回结果:

var returnCitySN = {"cip": "113.110.210.117", "cid": "440300", "cname": "广东省深圳市"};

屏蔽某个城市的IP

<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
  <script>
    function BlockTheUser(){
      //
      //do something
      //
      window.location = "http://www.npc.gov.cn/npc/c30834/201905/9a37c6ff150c4be6a549d526fd586122.shtml";
    }

      if("福建省泉州市" == returnCitySN.cname){
        BlockTheUser();
      }
  </script>

识别中文环境

<script type="text/javascript">
    function BlockTheUser(){
      window.location = "http://www.npc.gov.cn/npc/c30834/201905/9a37c6ff150c4be6a549d526fd586122.shtml";
    }
    function isUseChinese(){
      let lang = navigator.language;//Get default language of the browser
      if("zh-CN" == lang){
        return true;
      } else {
        return false;
      }
    }
    
    if(isUseChinese()){
      BlockTheUser();
    }

    $.get("https://ipinfo.io", function(response) {
      if("CN" == response.country){
        BlockTheUser();
      }
    }, "jsonp");
    
</script>

屏蔽指定ip段

<script>
var BlockIPList = ['54.187.2.231','103.24.65.175','113.87.0.134'];  //IP黑名单
function IsInBlockIPList(cipTarget){
  for(j = 0, len = BlockIPList.length; j < len; j++) {
	if(BlockIPList[j] == cipTarget){
	  return true;
	}
  }
  return false;
}

function BlockTheUser(){
  var currentUrl = window.location.href;
  if(currentUrl.indexOf("your-region-is-not-supported") == -1){
	 window.location = "https://你的域名.com/blogs/news/your-region-is-not-supported";  //跳转到你自己指定的页面
  }

  //6s后跳出本网站
  setTimeout(function(){ window.location = "http://www.npc.gov.cn/npc/c30834/201905/9a37c6ff150c4be6a549d526fd586122.shtml"; }, 6000);
}
function isUseChinese(){  //判断用户是否使用中文
  let lang = navigator.language;
  if("zh-CN" == lang){
	return true;
  } else {
	return false;
  }
}

$.get("https://ipinfo.io", function(response) {  //判断用户是否在中国
  if("CN" == response.country){
	BlockTheUser();
  }
}, "jsonp");

if(isUseChinese()){
  BlockTheUser();
}else if("福建省泉州市" == returnCitySN.cname){  //判断用户是否在泉州
  BlockTheUser();
}else if(IsInBlockIPList(returnCitySN.cip)){
  BlockTheUser();
}
</script>