zoukankan      html  css  js  c++  java
  • 3种方法判断手机浏览器跳转WAP手机网站

      随着移动设备的普及,企业的网络宣传已经不能局限在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.="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){
    //这是一个手机浏览器,可以跳转到手机版网页
    //header("Location: http://m.lixincpa.cn");
    echo "手机访问";
    }else{
    //这不是一个手机浏览器
    //header("Location: http://www.lixincpa.cn");
    echo "电脑访问";
    }
    ?>

  • 相关阅读:
    LeetCode 230. Kth Smallest Element in a BST
    LeetCode 114. Flatten Binary Tree to Linked List
    LeetCode 222. Count Complete Tree Nodes
    LeetCode 129. Sum Root to Leaf Numbers
    LeetCode 113. Path Sum II
    LeetCode 257. Binary Tree Paths
    Java Convert String & Int
    Java Annotations
    LeetCode 236. Lowest Common Ancestor of a Binary Tree
    LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/tanlingdangan/p/4282085.html
Copyright © 2011-2022 走看看