我們可以通過userAgent來判斷,比如檢測某些關(guān)鍵字,例如:AppleWebKit*****Mobile或AppleWebKit,需要注意的是有些瀏覽器的userAgent中并不包含AppleWebKit關(guān)鍵字,可能會是Linux,UCBrowser等等。
- <script type="text/javascript">
- var browser = {
- versions : function() {
- var u = navigator.userAgent, app = navigator.appVersion;
- return {//移動終端瀏覽器版本信息
- trident : u.indexOf('Trident') > -1, //IE內(nèi)核
- presto : u.indexOf('Presto') > -1, //opera內(nèi)核
- webKit : u.indexOf('AppleWebKit') > -1, //蘋果>內(nèi)核
- gecko : u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐內(nèi)核
- mobile : !!u.match(/AppleWebKit.*Mobile.*/)
- || !!u.match(/AppleWebKit/), //是否為移動終端
- ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios終端
- android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android終端或者uc瀏覽器
- iPhone : u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否為iPhone或者QQHD瀏覽器
- iPad: u.indexOf('iPad') > -1, //是否iPad
- webApp : u.indexOf('Safari') == -1,
- //是否web應(yīng)該程序,沒有頭部與底部
- google:u.indexOf('Chrome')>-1
- };
- }(),
- language : (navigator.browserLanguage || navigator.language).toLowerCase()
- }
- document.writeln("語言版本: "+browser.language);
- document.writeln(" 是否為移動終端: "+browser.versions.mobile);
- //其他的適配代碼(后綴名為.js 并引用至網(wǎng)頁)
- <!--
- //平臺、設(shè)備和操作系統(tǒng)
- var system = {
- win: false,
- mac: false,
- xll: false,
- ipad:false
- };
- //檢測平臺
- var p = navigator.platform;
- system.win = p.indexOf("Win") == 0;
- system.mac = p.indexOf("Mac") == 0;
- system.x11 = (p == "X11") || (p.indexOf("Linux") == 0);
- system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false;
- //跳轉(zhuǎn)語句,如果是手機訪問就自動跳轉(zhuǎn)到" "里的頁面
- if (system.win || system.mac || system.xll||system.ipad) {
- } else {
- window.location.href="http://m.starchuang.com/";
- }
- -->
- </script>
相關(guān)標簽:

