先上一个示例图:
我们用手机登陆新浪的时候,如果输入www.sina.com的时候,会自动跳转到这个页面,让你选择是手机触屏版还是电脑版,非常方便。那我们也仿照一个。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<script type="text/javascript" language="javascript"> Function.prototype.bind = function (bindObj, args) { var _self = this; return function () { return _self.apply(bindObj, [].concat(args)) } }; function setCookie(name, value, option) { var str = name + '=' + escape(value); if (option) { if (option.expireHours) { var d = new Date(); d.setTime(d.getTime() + option.expireHours * 3600 * 1000); str += '; expires=' + d.toGMTString(); } if (option.path) str += '; path=' + option.path; else str += '; path=/'; if (option.domain) str += '; domain=' + option.domain; if (option.secure) str += '; true'; } document.cookie = str; } function getCookie(name, defaultValue) { var re = new RegExp(name + '=([^;]*);?', 'gi'); var v = typeof defaultValue == "undefined" ? null : defaultValue; var r = re.exec(document.cookie) || []; return (r.length > 1 ? unescape(r[1]) : v) }; function $(id) { return "string" == typeof id ? document.getElementById(id) : id; }; /* * 智能机浏览器版本信息: * */ var browser = { versions: function () { var u = navigator.userAgent, app = navigator.appVersion; return { trident: u.indexOf('Trident') > -1, presto: u.indexOf('Presto') > -1, webKit: u.indexOf('AppleWebKit') > -1, gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, iPad: u.indexOf('iPad') > -1, webApp: u.indexOf('Safari') == -1, QQbrw: u.indexOf('MQQBrowser') > -1, ucLowEnd: u.indexOf('UCWEB7.') > -1, ucSpecial: u.indexOf('rv:1.2.3.4') > -1, ucweb: function () { try { return parseFloat(u.match(/ucweb\d+\.\d+/gi).toString().match(/\d+\.\d+/).toString()) >= 8.2 } catch (e) { if (u.indexOf('UC') > -1) { return true; } else { return false; } } } (), Symbian: u.indexOf('Symbian') > -1, ucSB: u.indexOf('Firefox/1.') > -1 }; } () } alert('QQbrw:' + browser.versions.QQbrw + '\nSymbian:' + browser.versions.Symbian + '\nandroid:' + browser.versions.android + '\niPhone:' + browser.versions.iPhone + '\nios:' + browser.versions.ios + '\nmobile:' + browser.versions.mobile + '\nucLowEnd:' + browser.versions.ucLowEnd + '\nucSB:' + browser.versions.ucSB + '\nucSpecial:' + browser.versions.ucSpecial + '\nucweb:' + browser.versions.ucweb + '\nwebApp:' + browser.versions.webApp + '\nwebKit:' + browser.versions.webKit + '\ngecko:' + browser.versions.gecko); var _gaq = _gaq || []; (function (win, browser, undefined) { var rf = document.referrer; if (rf === "" || rf.toLocaleLowerCase().indexOf(".junshishu.com") === -1) { var defaultJumpDomain = getCookie("defaultJumpDomain", ""); if (defaultJumpDomain == "www") { return; } if (defaultJumpDomain == "m") { window.location.href = "http://m.junshishu.com/"; return; } if (screen == undefined || screen.width < 810) { if (browser.versions.iPad == true) { return; } if (browser.versions.webKit == true || browser.versions.mobile == true || browser.versions.ios == true || browser.versions.iPhone == true || browser.versions.ucweb == true || browser.versions.ucSpecial == true) { win.location.href = "http://m.autohome.com.cn/tzh.html"; return; } if (browser.versions.Symbian) { win.location.href = "http://m.junshishu.com/"; } } } })(window, browser); </script>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" /> <meta content="yes" name="apple-mobile-web-app-capable" /> <meta content="black" name="apple-mobile-web-app-status-bar-style" /> <meta content="telephone=no" name="format-detection" /> <meta content="false" id="twcClient" name="twcClient" /> <title></title> <style> body{ font-family:"Microsoft Yahei",Tahoma,"SimSun"; background:#fafafa; -webkit-text-size-adjust:none;} body,div,dl,dt,dd,ul,ol,li,h1,h2,pre,blockquote{margin:0;padding:0;} a{ color:#0b337b; text-decoration:none;} .wrap{} /*提示*/ .tz{ padding:5px; font-size:18px;} .tz h2{ margin-bottom:10px; font-size:24px; font-weight:bold;} .tz div{ margin-bottom:10px; line-height:24px;} .tz div span{ color:#0b337b;} </style> </head> <body> <div class="tz"> <h2>铁血读书提示您:</h2> <div>iPhone,Android,Window Phone7系统手机用户,建议选择<span onclick="click_span_m()">触屏版</span></div> <div onclick="click_span_www()">您也可以直接访问<span>电脑版</span></div> </div> <script> Auto = {}; Auto["setCookie"] = function (name, value, option) { var str = name + '=' + escape(value); if (option) { if (option.expireHours) { var d = new Date(); d.setTime(d.getTime() + option.expireHours * 3600 * 1000); str += '; expires=' + d.toGMTString(); } if (option.path) str += '; path=' + option.path; else str += '; path=/'; if (option.domain) str += '; domain=' + option.domain; if (option.secure) str += '; true'; } document.cookie = str; } function click_span_m() { Auto.setCookie("defaultJumpDomain", "m", { expireHours: 168, domain: '.junshishu.com' }); window.location.href = "http://m.junshishu.com/3G/"; } function click_span_www() { Auto.setCookie("defaultJumpDomain", "www", { expireHours: 168, domain: '.junshishu.com' }); window.location.href = "http://www.junshishu.com"; } var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-30787185-1']); _gaq.push(['_setDomainName', '.junshishu.com']); _gaq.push(['_setAllowHash', false]); if (typeof(Worker) !== "undefined") { _gaq.push(['_setCustomVar',1,'User','Html5', 1]); } _gaq.push(['_trackPageview']); (function () { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://"); document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Fc3d5d12c0100a78dd49ba1357b115ad7' type='text/javascript'%3E%3C/script%3E")); </script> </body> </html>