JS判断手机访问函数

模板网 2014-09-11

函数is_mobile()手机端返回true,PC端返回false,根据返回值来判断时候是手机端访问,然后做相应的操作。

复制代码 代码如下:
<script>
function is_mobile() {
var regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i;
var u = navigator.userAgent;
if (null == u) {return true;}
var result = regex_match.exec(u);
if (null == result) {
return false
}else {
return true
}
}
if (is_mobile()) {
var weburl=window.location.href;
weburl=weburl.replace("http://www.xxx.com","http://m.xxx.com"); 
document.location.href= weburl;
</script>

<script>
function is_mobile() {
	var regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i;
	var u = navigator.userAgent;
	if (null == u) {return true;}
		var result = regex_match.exec(u);
	if (null == result) {
		return false
	}else {
		return true
	}	
}
	if (is_mobile()) {
	var weburl=window.location.href;
	weburl=weburl.replace("http://www.xxx.com","http://m.xxx.com"); 
	document.location.href= weburl;
</script>

相关文章

  1. 移动端全屏滚动插件 zepto.fullpage.js

    zepto.fullpage 是专注于移动端的 fullPag e.js ,依赖 Ze pto 。 下载地址: https://github.com/yanhaijing/zepto.fullpage 功能概述 可实现移动端的单页滚动效果,可自定义参数,提供回调接口,和公开接口。 兼容性 Ios4+ Andriod2.3+(未全部覆盖) 快速

  2. nTabs纯JS实现的标签切换功能代码

    nTabs纯JS实现的标签切换功能代码,使用非常方便。

  3. 使用新浪获取IP所在地接口做网站跳转

    使用新浪获取IP接口做网站跳转 title使用新浪获取IP接口做网站跳转/title !--引用新浪IP获取接口-- script type=text/javascript src=http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js/script !--引用新浪IP获取接口-- script type=text/javas

  4. 不错的医院页面挂号下拉效果

    不错的医院页面挂号下拉效果,仅供参考。 效果截图:

  5. JS判断手机访问跳转到手机站使用百度uaredirect.js

    使用了百度SIteApp提供的一个判断手机访客的js代码uaredirect.js,来实现当用户使用手机访问我们的PC站的时候自动跳转到我们的手机站。 用法非常简单: 第一段是引用百度的uaredirect.js,本人还是喜欢将代码保存到本地,代码量也不是很大。 第二段是使用代

随机推荐

  1. 移动端全屏滚动插件 zepto.fullpage.js

    zepto.fullpage 是专注于移动端的 fullPag e.js ,依赖 Ze pto 。 下载地址: https://github.com/yanhaijing/zepto.fullpage 功能概述 可实现移动端的单页滚动效果,可自定义参数,提供回调接口,和公开接口。 兼容性 Ios4+ Andriod2.3+(未全部覆盖) 快速

  2. nTabs纯JS实现的标签切换功能代码

    nTabs纯JS实现的标签切换功能代码,使用非常方便。

  3. 使用新浪获取IP所在地接口做网站跳转

    使用新浪获取IP接口做网站跳转 title使用新浪获取IP接口做网站跳转/title !--引用新浪IP获取接口-- script type=text/javascript src=http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js/script !--引用新浪IP获取接口-- script type=text/javas

  4. 不错的医院页面挂号下拉效果

    不错的医院页面挂号下拉效果,仅供参考。 效果截图:

  5. JS判断手机访问跳转到手机站使用百度uaredirect.js

    使用了百度SIteApp提供的一个判断手机访客的js代码uaredirect.js,来实现当用户使用手机访问我们的PC站的时候自动跳转到我们的手机站。 用法非常简单: 第一段是引用百度的uaredirect.js,本人还是喜欢将代码保存到本地,代码量也不是很大。 第二段是使用代