学习资料_文档下载_软件应用_程序模板

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

搜索
查看: 2885|回复: 0

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

[复制链接]

295

主题

297

帖子

787

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
787
发表于 2020-3-22 03:03:07 | 显示全部楼层 |阅读模式
  随着移动设备的普及,企业的网络宣传已经不能局限在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.l.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.lixin.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|^*****|^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.lixin.cn");
echo "手机访问";
}else{
//这不是一个手机浏览器
//header("Location: http://www.lixin.cn");
echo "电脑访问";
}
?>

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

HI223.COM 举报邮箱:345306361@qq.com

GMT+8, 2025-7-3 21:00 , Processed in 0.066721 second(s), 18 queries .

Powered by HI223分享社区 2.3

© 2019-2020 Hi223 All Right Reserved

渝公网安备 50022502000358号

渝ICP备14008270号-3