手機訪問pc網站時使用js自動跳轉到移動網站域名對應的頁面鏈接javascript代碼。這是比較典型的跳轉需求,當用戶使用手機打開pc網站鏈接時,希望能跳轉到移動網站對應的頁面,適用於網站使用兩個域名區分pc,移動端的情況。比如www.域名.com是電腦站,m.域名.com是移動站這樣兩個域名,並且目錄結構url是一樣的,只有域名不一樣。
此代碼當手機訪問pc站時跳轉到移動站的對應頁面,代碼放在pc站上。最好放在<head>下面第一位,最先載入這段js,跳轉更快一點。
<script type=”text/javascript”>
function browserRedirect() {
var hz=location.pathname;
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=”http://m.reaer.com”+hz+”#www”;
} else {
}
}
browserRedirect();
</script>
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/216908.html
微信掃一掃
支付寶掃一掃