手機(jī)訪問PC網(wǎng)站自動跳轉(zhuǎn)到手機(jī)版
隨著智能手機(jī)的流行,4G時代來臨,手機(jī)用戶越來越多,在生活中甚至手機(jī)比電腦用的還多,當(dāng)前開發(fā)的網(wǎng)站大都是PC和WAP版并存,但是很少有用戶愿意去記住一個網(wǎng)站的兩個端的不同域名,所以需要我們做一些設(shè)置,在用戶訪問首頁的時候,進(jìn)行分析跳轉(zhuǎn),現(xiàn)將網(wǎng)上流行的幾種方式匯總?cè)缦拢M麑Υ蠹矣杏茫?br/>
第一種方式:
推薦,簡單易用,親測可以正常使用,代碼如下:
<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script>
<script type="text/javascript">uaredirect("你的手機(jī)版網(wǎng)址");</script>
第二種方式:
添加js代碼,代碼如下:
<script type="text/javascript">
try {
var urlhash = window.location.hash;
if (!urlhash.match("fromapp")) {
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) {
window.location = "你的手機(jī)版地址";
}
}
}
catch (err) {
}
</script>
第三種方式:
添加js代碼,代碼如下:
<script type="text/javascript">
function urlredirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
if ((sUserAgent.match(/(ipod|iphone os|midp|ucweb|android|windows ce|windows mobile)/i))) {
//PC跳轉(zhuǎn)移動端
var thisUrl = window.location.href;
//此處是在PC鏈接后自動添加手機(jī)版前綴,根據(jù)項(xiàng)目自行變更,我的是m開頭
window.location.href = thisUrl.substr(0,thisUrl.lastIndexOf('/') + 1) + 'mobile/';
}
}
urlredirect();
</script>
第四種方式:
添加js代碼,代碼如下:
<script type="text/javascript">
function mobile_device_detect(url) {
var thisOS = navigator.platform;
var os = new Array("iPhone", "iPod", "iPad", "android", "Nokia",
"SymbianOS", "Symbian", "Windows Phone", "Phone",
"Linux armv71", "MAUI", "UNTRUSTED/1.0", "Windows CE",
"BlackBerry", "IEMobile");
for ( var i = 0; i < os.length; i++) {
if (thisOS.match(os[i])) {
window.location = url;
}
}
// 因?yàn)橄喈?dāng)部分的手機(jī)系統(tǒng)不知道信息,這里是做臨時性特殊辨認(rèn)
if (navigator.platform.indexOf('iPad') != -1) {
window.location = url;
}
// 做這一部分是因?yàn)锳ndroid手機(jī)的內(nèi)核也是Linux
// 但是navigator.platform顯示信息不盡相同情況繁多,因此從瀏覽器下手,即用navigator.appVersion信息做判斷
var check = navigator.appVersion;
if (check.match(/linux/i)) {
// X11是UC瀏覽器的平臺 ,如果有其他特殊瀏覽器也可以附加上條件
if (check.match(/mobile/i) || check.match(/X11/i)) {
window.location = url;
}
}
// 類in_array函數(shù)
Array.prototype.in_array = function(e) {
for (i = 0; i < this.length; i++) {
if (this[i] == e)
return true;
}
return false;
}
}
mobile_device_detect("你的手機(jī)版地址");
</script>
如果您的問題還未解決可以聯(lián)系站長付費(fèi)協(xié)助。

有問題可以加入技術(shù)QQ群一起交流學(xué)習(xí)
本站vip會員 請加入無憂模板網(wǎng) VIP群(50604020) PS:加入時備注用戶名或昵稱
普通注冊會員或訪客 請加入無憂模板網(wǎng) 技術(shù)交流群(50604130)
客服微信號:15898888535
聲明:本站所有文章資源內(nèi)容,如無特殊說明或標(biāo)注,均為采集網(wǎng)絡(luò)資源。如若內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系站長刪除。