내가 자꾸 까먹어서 쓰는 개발 이야기/JavaScript
모바일 접속 체크하는 스크립트
FIL.
2013. 2. 12. 16:23
728x90
var Device = {ENV_DESKTOP: 'desktop',ENV_PHONE: 'phone',ENV_TABLET: 'tablet',ENV_PORTRAIT: 'portrait',ENV_LANDSCAPE: 'landscape',agent: {mobile: (/iphone|ipod|android|blackberry|opera|mini|windows\sce|palm|smartphone|iemobile/i.test(window.navigator.userAgent.toLowerCase())),tablet: (/ipad|android|android\s3\.0|xoom|sch-i800|playbook|tablet|kindle/i.test(window.navigator.userAgent.toLowerCase()))},resolutions: {phones: {width: [240, 960],height: [320, 800]},tablets: {width: [1024, 2048],height: [60, 1536]}},support: {orientation: ('DeviceOrientationEvent' in window),motion: ('DeviceMotionEvent' in window),touch: ('ontouchstart' in window)},standalone: ('standalone' in window.navigator),detect: function () {var that = this,inRange = function (type) {var size = that.screenSize(),collection = that.resolutions[type];return ((size.width >= collection.width[0] && size.width <= collection.width[1]) && (size.height >= collection.height[0] && size.height <= collection.height[1]));};if (this.agent.mobile && inRange('phones')) {this.type = this.ENV_PHONE;} else {this.type = (this.agent.tablet && inRange('tablets')) ? this.ENV_TABLET : this.ENV_DESKTOP;}},orientation: function () {return this.support.orientation ? (window.orientation !== 0 ? this.ENV_PORTRAIT : this.ENV_LANDSCAPE) : null;},screenSize: function () {var portrait = (this.orientation() === this.ENV_PORTRAIT);return {width: portrait ? screen.height : screen.width,height: portrait ? screen.width : screen.height};},hideKeyboard: function () {document.activeElement.blur();},hideUrlBar: function () {if (this.type === this.ENV_PHONE) {setTimeout(function () {window.scrollTo(0, 1);}, 0);}}};
호출방법은 나중에 테스트 해보고 다시...
출처 : http://ultteky.egloos.com/10983523