在群里遇到个人说跳转存在问题,求解决;给我的代码是这样的:
他想实现就是限制:北京,上海,重庆 跳转;
很简单,逻辑运算符用错了;改为&&,理论上说应该正常工作了。
- <script type=“text/javascript” src=“http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js“></script>
- <script language=“javascript”>
- function checkMobile() {
- var pda_user_agent_list = new Array(“2.0 MMP”, “240320”, “AvantGo”, “BlackBerry”, “Blazer”,
- “Cellphone”, “Danger”, “DoCoMo”, “Elaine/3.0”, “EudoraWeb”, “hiptop”, “IEMobile”, “KYOCERA/WX310K”, “LG/U990”,
- “MIDP-2.0”, “MMEF20”, “MOT-V”, “NetFront”, “Newt”, “Nintendo Wii”, “Nitro”, “Nokia”,
- “Opera Mini”, “Opera Mobi”,
- “Palm”, “Playstation Portable”, “portalmmm”, “Proxinet”, “ProxiNet”,
- “SHARP-TQ-GX10”, “Small”, “SonyEricsson”, “Symbian OS”, “SymbianOS”, “TS21i-10”, “UP.Browser”, “UP.Link”,
- “Windows CE”, “WinWAP”, “Androi”, “iPhone”, “iPod”, “iPad”, “Windows Phone”, “HTC”);
- var pda_app_name_list = new Array(“Microsoft Pocket Internet Explorer”);
- var user_agent = navigator.userAgent.toString();
- for (var i = 0; i < pda_user_agent_list.length; i++) {
- if (user_agent.indexOf(pda_user_agent_list[i]) >= 0) {
- return true;
- }
- }
- var appName = navigator.appName.toString();
- for (var i = 0; i < pda_app_name_list.length; i++) {
- if (user_agent.indexOf(pda_app_name_list[i]) >= 0) {
- return true;
- }
- }
- return false;
- }
- if (checkMobile()) {
- var t = remote_ip_info;
- if (( t.province != ’北京’ ) || ( t.province != ’重庆’) || (t.province != ’上海’)) {
- window.location.href = (“http://sp.ffxxff.com“);
- }
- }
- </script>
实际表现却不是这样,居然进了if;在console里执行条件语句,结果确实是false;靠,什么情况难道是bug?
增加了一些调试信息:
发现条件句的返回的结果是true,就更不解了;怎么会是true,最后一项分明是false。
折腾了好久,发现是编码问题:
加上:`
- <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” /> `,浏览器编码再改为自动识别就ok了。
- <head>
- <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />
- </head>
- <script type=“text/javascript” src=“http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js“></script>
- <script language=“javascript”>
- function checkMobile() {
- var pda_user_agent_list = new Array(“2.0 MMP”, “240320”, “AvantGo”, “BlackBerry”, “Blazer”,
- “Cellphone”, “Danger”, “DoCoMo”, “Elaine/3.0”, “EudoraWeb”, “hiptop”, “IEMobile”, “KYOCERA/WX310K”, “LG/U990”,
- “MIDP-2.0”, “MMEF20”, “MOT-V”, “NetFront”, “Newt”, “Nintendo Wii”, “Nitro”, “Nokia”,
- “Opera Mini”, “Opera Mobi”,
- “Palm”, “Playstation Portable”, “portalmmm”, “Proxinet”, “ProxiNet”,
- “SHARP-TQ-GX10”, “Small”, “SonyEricsson”, “Symbian OS”, “SymbianOS”, “TS21i-10”, “UP.Browser”, “UP.Link”,
- “Windows CE”, “WinWAP”, “Androi”, “iPhone”, “iPod”, “iPad”, “Windows Phone”, “HTC”);
- var pda_app_name_list = new Array(“Microsoft Pocket Internet Explorer”);
- var user_agent = navigator.userAgent.toString();
- for (var i = 0; i < pda_user_agent_list.length; i++) {
- if (user_agent.indexOf(pda_user_agent_list[i]) >= 0) {
- return true;
- }
- }
- var appName = navigator.appName.toString();
- for (var i = 0; i < pda_app_name_list.length; i++) {
- if (user_agent.indexOf(pda_app_name_list[i]) >= 0) {
- return true;
- }
- }
- return false;
- }
- //if (checkMobile()) {
- var t = remote_ip_info;
- alert(t.province);
- console.log(t.province);
- alert((t.province != ’北京’) && (t.province != ’重庆’) && (t.province != ’上海’));
- if(((t.province != ’北京’) && (t.province != ’重庆’) && (t.province != ’上海’))) {
- console.log(“a”);
- console.log((t.province != ’北京’));
- console.log(“b”);
- console.log((t.province != ’重庆’));
- console.log(“c”);
- console.log(t.province);
- console.log(EncodeUtf8(“上海”));
- console.log((EncodeUtf8(t.province) != EncodeUtf8(“上海”)));
- console.log(“d”);
- console.log((t.province != ’北京’) && (t.province != ’重庆’));
- console.log(“e”);
- console.log((t.province != ’北京’) && (t.province != ’重庆’) && (t.province != ’上海’));
- console.log(“f”);
- alert(t.province != ’北京’ && t.province != ’重庆’ && t.province != ’上海’);
- alert(typeof(t.province != ’北京’ && t.province != ’重庆’ && t.province != ’上海’));
- alert((t.province != ’北京’ && t.province != ’重庆’ && t.province != ’上海’).toString());
- // window.location.href = (“http://sp.ffxxff.com“);
- }
- //}
- function EncodeUtf8(s1)
- {
- var s = escape(s1);
- var sa = s.split(“%”);
- var retV =“”;
- if(sa[0] != “”)
- {
- retV = sa[0];
- }
- for(var i = 1; i < sa.length; i ++)
- {
- if(sa[i].substring(0,1) == “u”)
- {
- retV += Hex2Utf8(Str2Hex(sa[i].substring(1,5)));
- }
- else retV += “%” + sa[i];
- }
- return retV;
- }
- function Str2Hex(s)
- {
- var c = “”;
- var n;
- var ss = “0123456789ABCDEF”;
- var digS = “”;
- for(var i = 0; i < s.length; i ++)
- {
- c = s.charAt(i);
- n = ss.indexOf(c);
- digS += Dec2Dig(eval(n));
- }
- //return value;
- return digS;
- }
- function Dec2Dig(n1)
- {
- var s = “”;
- var n2 = 0;
- for(var i = 0; i < 4; i++)
- {
- n2 = Math.pow(2,3 - i);
- if(n1 >= n2)
- {
- s += ’1’;
- n1 = n1 - n2;
- }
- else
- s += ’0’;
- }
- return s;
- }
- function Dig2Dec(s)
- {
- var retV = 0;
- if(s.length == 4)
- {
- for(var i = 0; i < 4; i ++)
- {
- retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
- }
- return retV;
- }
- return -1;
- }
- function Hex2Utf8(s)
- {
- var retS = “”;
- var tempS = “”;
- var ss = “”;
- if(s.length == 16)
- {
- tempS = “1110” + s.substring(0, 4);
- tempS += “10” + s.substring(4, 10);
- tempS += “10” + s.substring(10,16);
- var sss = “0123456789ABCDEF”;
- for(var i = 0; i < 3; i ++)
- {
- retS += “%”;
- ss = tempS.substring(i 8, (eval(i)+1)8);
- retS += sss.charAt(Dig2Dec(ss.substring(0,4)));
- retS += sss.charAt(Dig2Dec(ss.substring(4,8)));
- }
- return retS;
- }
- return “”;
- }
- </script>