基于JS实现移动端访问PC端页面时跳转到对应的移动端网页

不想通过CSS自适应在PC端和移动端分别显示不同的样式,那么只能通过在移动端访问PC端网页时跳转到对应的移动端网页了,那么怎么跳转呢,网上也有很多文章说明,以下实现思路经过小编测试过,需要的朋友可以参考下

不想通过CSS自适应在PC端和移动端分别显示不同的样式,那么只能通过在移动端访问PC端网页时跳转到对应的移动端网页了,那么怎么跳转呢,网上也有很多文章说明,以下实现思路经过小编测试过,放心使用。

1.效果图

PC端访问显示:

移动端访问显示:

2.实现:

不考虑移动端搜索引擎优化的话,只需要通过JS判断是否移动端,然后确定是否跳转到指定页面就行了,主要JS如下:

 //判断是否移动端,如果是则跳转到指定的URL地址 function browserRedirect(url) { //只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值 var sUserAgent = navigator.userAgent.toLowerCase(); 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 (bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) { window.location.replace(url); } }

然后在页面引用JS,调用方法就行了:

 

html中文网友情提醒:大家可以用PC端和移动端访问测试页面演示效果哦!

pc自动跳移动端

 (function() { if (/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile|Opera Mini/i.test(navigator.userAgent)) { var siteName = window.location.pathname; if (url.indexOf("?pc") <0) { try { if (typeof siteName !== "undefined") { window.location.href = "https://m.0133.cn" + siteName } } catch (e) {} } } })();

移动端自动跳pc

 ;(function() { var reWriteUrl = function(url) { if (url) { var Splits = url.split("/"), siteName = window.location.pathname; if (typeof siteName !== "undefined") { return "https://www.0133.cn" + siteName } } }; if (!/Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile|Opera Mini/i.test(navigator.userAgent)) { var url = window.location.href; var pathname = window.location.pathname; if (url.indexOf("?m") <0) { try { window.location.href = reWriteUrl(url) } catch(e) {} } } })();

关于本文给大家介绍的基于JS实现移动端访问PC端页面时跳转到对应的移动端网页就给大家介绍这么多,希望对大家有所帮助!

以上就是基于JS实现移动端访问PC端页面时跳转到对应的移动端网页的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » JavaScript 教程