昨天晚上用户给出一个需求,就是想实现网站面对不同地区的用户的时候,可以实现根据地区IP不同进行不同跳转也就是访问不同的网站页面内容。这个问题其实是比较常见的,我们前面361模板给大家用新浪IP库介绍过这样的问题《融合新浪IP库和搜狐IP库获取访客IP地址和IP归属地信息并无刷新显示》,这次用户的需求是:
1、使用百度API借口实现
2、PC电脑端访问直接跳www.vevb.com
3、移动端(北京上海广州深圳苏州杭州)跳www.tao2t.net
4、其它正常
下面是实现代码
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta charset="utf-8" /> <script> function tiao(c) {console.log("City vistor:"+c.content.cname);if( c.content.cname.indexOf("/u5317/u4eac")>-1 || c.content.cname.indexOf("/u4e0a/u6d77")>-1 || c.content.cname.indexOf("/u5e7f/u5dde")>-1 || c.content.cname.indexOf("/u6df1/u5733")>-1 || c.content.cname.indexOf("/u82cf/u5dde")>-1 || c.content.cname.indexOf("/u676d/u5dde")>-1){//跳转console.log("跳到WWW.TAO2T.NET");//跳转到TAO2T去location.href = "http://www.tao2t.net"; } }function uaredirect() {var n = navigator.userAgent.toLowerCase();if (n.indexOf('android')>-1 || n.indexOf('iphone')>-1 || n.indexOf('iPhone')>-1 || n.indexOf('ipod')>-1 || n.indexOf('ipad')>-1 || n.indexOf('ios')>-1){tiao(c);}else {//PC端跳转到www.vevb.com location.href = "http://www.vevb.com";}}uaredirect(); </script> <script src="https://api.map.baidu.com/?qt=dec&oue=1&callback=tiao"></script> </head><body></body></html>
这里需要注意的是:
因为百度是使用十六进制表示的中文地区名称,因此需要转换
转换的地址:http://www.vevb.com/tool/changecode.html
以上就是【精】使用百度API接口实现不同地区访客跳转不同地址 不同地区IP跳转不同网址的全部内容,希望对大家的学习和解决疑问有所帮助,也希望大家多多支持武林网。新闻热点
疑难解答