上级领导检查网站?百度竞价临时检查?你总会遇到各式各样的临时问题。以下代码可以实现指定地区(城市)禁止访问。原理是根据IP地址归属地区来进行筛选,进行跳转。
<script src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js" charset="GB2312" ></script> <script type=text/javascript> if(remote_ip_info.city =='成都'){ window.location.href="跳转链接"; } </script>
由于http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪接口目前用不了,所以上面代码没法执行,下面讲用126网易接口的
代码1:
<!doctype html> <html> <head> <meta charset="gb2312"> <title>无标题文档</title> <script type="text/javascript" src="http://ip.ws.126.net/ipquery"></script> <script> var province=localAddress.province;//获取所在省,比如广东省 var city=localAddress.city;//获取所在市,比如广州市 //判断省 if(province.indexOf('广东') != -1){ alert(province); }else{ } //判断市 if(city.indexOf('广州') != -1){ alert(city); }else{ } </script> </head> <body> <p>12345678</p> </body> </html>
代码2:
<!doctype html> <html> <head> <meta charset="gb2312"> <title>无标题文档</title> <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script> <script> $.getScript('http://ip.ws.126.net/ipquery', function(){ console.log(localAddress.city) if(localAddress.city.indexOf('北京') != -1||localAddress.city.indexOf('杭州') != -1||localAddress.city.indexOf('上海') != -1||localAddress.city.indexOf('广州') != -1||localAddress.city.indexOf('深圳') != -1||localAddress.city.indexOf('东莞') != -1||localAddress.city.indexOf('房山') != -1){ window.location.href = 'http://www.baidu.com'; }else{ return; } console.log(localAddress.province) if(localAddress.province.indexOf('福建') != -1){ window.location.href = 'http://www.baidu.com'; }else{ return; } }); </script> </head> <body> <p>123456789</p> </body> </html>