3种方法判断手机浏览器跳转WAP手机网站
隨著移動(dòng)設(shè)備的普及,企業(yè)的網(wǎng)絡(luò)宣傳已經(jīng)不能局限在PC端,而需要同時(shí)在移動(dòng)端有所建樹。對(duì)于公司網(wǎng)站來說,以前都是做的PC端的,當(dāng)然手機(jī)等移動(dòng)端也可以訪問,但是用戶體驗(yàn)肯定不如完全適合的手機(jī)端來的方便。我們?cè)诮o自己的網(wǎng)站做了WAP手機(jī)網(wǎng)站之后,如果有用戶通過手機(jī)訪問我們的企業(yè)頂級(jí)域名網(wǎng)站,那就判斷跳轉(zhuǎn)到專為的手機(jī)站點(diǎn)。
第一種:直接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="http://m.lixincpa.cn(更換成自己的WAP網(wǎng)站)";
}
}
}
catch(err)
{
}</script>
第二種,稍微長一些,引用
function uaredirect(murl){
try {
if(document.getElementById("bdmark") != null){
return;
}
var urlhash = window.location.hash;
if (!urlhash.match("fromapp")){
if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) {
location.replace(murl);
}
}
} catch(err){}
}
上述的腳本保持到mobile.js文件,然后在需要調(diào)用的頁面引用下面2個(gè)JS調(diào)用文件。
<script src="mobile.js" type="text/javascript"></script>
<script type="text/javascript">uaredirect("http://m.lixincpa.cn/(更換成自己的WAP網(wǎng)站)");</script>
兩種方法都可以使用,我在安卓和IPHONE測試可行。
下面給大家分享一段php中判斷電腦訪問還是手機(jī)訪問的代碼:
<?php
//手機(jī)網(wǎng)頁跳轉(zhuǎn)
//如果檢測到訪問的瀏覽器為下列一個(gè)指定的移動(dòng)瀏覽器 則返回true
function is_mobile(){
$regex_match="/(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|";
$regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
$regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
$regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|";
$regex_match.="jigs browser|hiptop|^ucweb|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220";
$regex_match.=")/i";
return isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']) or preg_match($regex_match, strtolower($_SERVER['HTTP_USER_AGENT']));
}
$is_mobile=is_mobile();
if($is_mobile){
//這是一個(gè)手機(jī)瀏覽器,可以跳轉(zhuǎn)到手機(jī)版網(wǎng)頁
//header("Location: http://m.lixincpa.cn");
echo "手機(jī)訪問";
}else{
//這不是一個(gè)手機(jī)瀏覽器
//header("Location: http://www.lixincpa.cn");
echo "電腦訪問";
}
?>
總結(jié)
以上是生活随笔為你收集整理的3种方法判断手机浏览器跳转WAP手机网站的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 赛码网算法: 上台阶 ( python3
- 下一篇: 为何把2点半比作是神奇的2点半? 为什么