您的位置:首页 > 运维架构 > 网站架构

3种方法判断手机浏览器跳转WAP手机网站

2015-02-09 17:52 417 查看
  随着移动设备的普及,企业的网络宣传已经不能局限在PC端,而需要同时在移动端有所建树。对于公司网站来说,以前都是做的PC端的,当然手机等移动端也可以访问,但是用户体验肯定不如完全适合的手机端来的方便。我们在给自己的网站做了WAP手机网站之后,如果有用户通过手机访问我们的企业顶级域名网站,那就判断跳转到专为的手机站点。
  
  第一种:直接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网站)";
  
  }
  
  }
  
  }
  
  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文件,然后在需要调用的页面引用下面2个JS调用文件。
  
  <script src="mobile.js" type="text/javascript"></script>
  
  <script type="text/javascript">uaredirect("http://m.lixincpa.cn/(更换成自己的WAP网站)");</script>
  
  两种方法都可以使用,我在安卓和IPHONE测试可行。

  下面给大家分享一段php中判断电脑访问还是手机访问的代码:
<?php
//手机网页跳转
//如果检测到访问的浏览器为下列一个指定的移动浏览器 则返回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.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*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){
//这是一个手机浏览器,可以跳转到手机版网页
//header("Location: http://m.lixincpa.cn"); echo "手机访问";
}else{
//这不是一个手机浏览器
//header("Location: http://www.lixincpa.cn"); echo "电脑访问";
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: