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. html5输入框正则验证

    html5输入框正则验证,输入框禁止粘贴,禁止右键菜单。

  2. 假404效果代码

    假404效果代码,使用frameset框架覆盖原网页内容,实现网页打不开的假象,直接在网站头部引用404.js即可。 代码下载: 假404效果代码 效果截图:

  3. 关闭网页时弹出对话框JS代码

    关闭网页时弹出对话框JS代码,当访客要关闭网页时弹出挽留对话框,用户体验上差了一点,但是据统计访客停留率还是挺高的。 代码效果:

  4. 使用新浪获取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

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

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

随机推荐

  1. html5输入框正则验证

    html5输入框正则验证,输入框禁止粘贴,禁止右键菜单。

  2. 假404效果代码

    假404效果代码,使用frameset框架覆盖原网页内容,实现网页打不开的假象,直接在网站头部引用404.js即可。 代码下载: 假404效果代码 效果截图:

  3. 关闭网页时弹出对话框JS代码

    关闭网页时弹出对话框JS代码,当访客要关闭网页时弹出挽留对话框,用户体验上差了一点,但是据统计访客停留率还是挺高的。 代码效果:

  4. 使用新浪获取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

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

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