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>
相关文章
- JS闪屏代码,闪瞎你的眼睛
JS恶搞代码,页面底色在黑与白之间快速切换,产生闪烁效果。JS闪屏代码,闪瞎你的眼睛。
- html5输入框正则验证
html5输入框正则验证,输入框禁止粘贴,禁止右键菜单。
- QQ添加好友代码
QQ添加好友代码 复制代码 代码如下: a href=tencent://AddContact/?fromId=50fromSubId=1subcmd=alluin=12345678加个好友吧/a
- 空间绑定多个域名 添加统计代码的方法
医疗建站中很多时候我们会在一个空间上绑定多个域名,但是希望给每个域名单独添加统计代码,下面医疗模板库www.120muban.com为大家带来实现这种需求的方法。 其实只需要通过JS来判断当前的网页域名,根据域名的不同输出不同的统计代码即可; 复制代码 代码如
- 不错的医院页面挂号下拉效果
不错的医院页面挂号下拉效果,仅供参考。 效果截图:
随机推荐
- JS闪屏代码,闪瞎你的眼睛
JS恶搞代码,页面底色在黑与白之间快速切换,产生闪烁效果。JS闪屏代码,闪瞎你的眼睛。
- html5输入框正则验证
html5输入框正则验证,输入框禁止粘贴,禁止右键菜单。
- QQ添加好友代码
QQ添加好友代码 复制代码 代码如下: a href=tencent://AddContact/?fromId=50fromSubId=1subcmd=alluin=12345678加个好友吧/a
- 空间绑定多个域名 添加统计代码的方法
医疗建站中很多时候我们会在一个空间上绑定多个域名,但是希望给每个域名单独添加统计代码,下面医疗模板库www.120muban.com为大家带来实现这种需求的方法。 其实只需要通过JS来判断当前的网页域名,根据域名的不同输出不同的统计代码即可; 复制代码 代码如
- 不错的医院页面挂号下拉效果
不错的医院页面挂号下拉效果,仅供参考。 效果截图: