手机端和PC端互相跳转对应链接 加载不同的js 小白前端实用攻略 手机端和PC端“纠纷”多,响应式设计、自适应设计中经常会碰到很多问题。 小白懂的不多,但小白不喜欢迷茫,分享我的经验吧! 手机端和PC端互相切换时跳转到不同的链接,代码如下: 移动端加载代码: function browserRedirect() { var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"; var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os"; var bIsMidp = sUserAgent.match(/midp/i) == "midp"; var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"; var bIsAndroid = sUserAgent.match(/android/i) == "android"; var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; if (bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) { } else { window.location.href="index.html"; //跳转到pc端对应页面 } } browserRedirect(); 上述代码写在移动端文件中。 解读:前面是一段判断,判断是不是移动端。(If语句)是,不做处理;不是(else),则跳转至PC端页面地址(index.html,可根据你的路径填写)。 PC端加载代码: function browserRedirect() { var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"; var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os"; var bIsMidp = sUserAgent.match(/midp/i) == "midp"; var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"; var bIsAndroid = sUserAgent.match(/android/i) == "android"; var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; if(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) { window.location.href = "index_m.html"; //跳转到移动端对应页面 } else { } } browserRedirect(); 上述代码写在PC端文件中。 解读:代码基本一样,If语句不同。判断是移动端,跳转至移动端页面地址;不是(else),不做处理。 此代码亲测并应用多次,真实可用,亲可收藏! 举一反三: 同样的代码,判断语句中,可做很多事情,例如可加载不同js、替换某个图片等。 加载不同的js: function browserRedirect() { var sUserAgent = navigator.userAgent.toLowerCase(); var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"; var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os"; var bIsMidp = sUserAgent.match(/midp/i) == "midp"; var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"; var bIsAndroid = sUserAgent.match(/android/i) == "android"; var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; if(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || 本文来源:https://www.wddqw.com/doc/7b4968192aea81c758f5f61fb7360b4c2f3f2acb.html