应用例子: https://zhan.cn.sj42.com/ (如果网站是https可以使用这个: https://api.mnjc.com.cn/ip)
注:不写IP地址会自动获取客户端IP
js核心代码: if(getCookie('citycode') > 0) { window.location = "https://zhan.cn.sj42.com/" + getCookie('citycode') + '/' } else { $.ajax({ url: "http://www.twoir.com/api/ip", type: "get", success: function (res) { const {status, result} = res;
if (0 === status) {
if(result.citycode > 0) { console.log(result.citycode) setCookie("citycode", result.citycode, 3, "/")//设置cookie window.location = "https://zhan.cn.sj42.com/" + result.citycode + '/' } } }, error: function () {
} }); }
|