Skip to content

Instantly share code, notes, and snippets.

@cssmagic
Last active March 11, 2018 13:18
Show Gist options
  • Save cssmagic/6238619 to your computer and use it in GitHub Desktop.
Save cssmagic/6238619 to your computer and use it in GitHub Desktop.
让手机用户自动跳转到移动版网站。
var _ua = _ua || {};
_ua.str = navigator.userAgent.toLowerCase();
_ua.isAndroid = _ua.str.indexOf('android') > -1 && _ua.str.indexOf('mobile') > -1;
_ua.isIPhone = /\(i(?:phone|pod);/.test(_ua.str);
if (_ua.isAndroid || _ua.isIPhone) {
location.href = 'http://m.example.com/';
}
@cssmagic
Copy link
Author

一段简单的脚本,可以把手机用户(Android / iPhone / iPod)重定向到移动版网站。不过在正常情况下,建议你在服务器端做类似的工作。

@dannyge
Copy link

dannyge commented Sep 28, 2017

为什么在建议在服务端做呢?

@noangel2014
Copy link

在服务端跳转对蜘蛛更友好,效率也更高

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment