Skip to content

Commit ab77cd8

Browse files
committed
browser detection
1 parent 92b916b commit ab77cd8

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

lib/BrowserDetection.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/* ============================================================================
2+
3+
>4SESz., _, ,gSEE2zx.,_ .azx ,w.
4+
@Sh. "QEE3. JEE3. &ss `*4EEEEE2x.,_ "EEV ,aS5^;dEEEE2x., VEEF _
5+
\E2`_,gF4EEEx.?jF EE5L `*4EEEEE2zpn..ZEEF ;sz. `*EEESzw.w* '|EE.
6+
,dEEF `"?j] _,_ ,_ _, _,. L.EEEF !EEF _,, `"`` EE7 ,,_
7+
:EEE7 ,ws,`|EEL`JEEL`JEE)`JEEL zE5` E3. / [EE3 ,w. zE2` Ek .zE5^JZE3.,6EF [3
8+
{EEE. VEE7.EE2 AE3. EEV ZEEL{EEL ws. ; [EE1 EEEt{E3. JEELEE3. JE5LJEEF ,w,
9+
\EEk,,>^ JEEL,@EEF ZE5L,ZE5^ "Q3. V2`. \EEk,,J' "Q[ yE2^ VE[_zEE[,"QEL V5F
10+
,ss :EE7 ;EEF L,szzw.. ,., ``
11+
\E5.,E5F EE1. /; ``*4EEEZhw._ EEEL
12+
```` `` JEEE. `"45EEEhw.,,,]
13+
14+
From 2010 till infinity
15+
typecode-js v 0.1
16+
*/
17+
18+
define(['jquery', 'NIseed'], function($) {
19+
20+
var window = this,
21+
NI = window.NI;
22+
23+
function BrowserDetection(options) {
24+
var o, elements;
25+
26+
o = $.extend({
27+
orientation_listen: false,
28+
detect_touch: true
29+
}, options);
30+
31+
elements = {
32+
body:$('body')
33+
};
34+
35+
function init(){
36+
var userAgent = navigator.userAgent;
37+
var appVersion = navigator.appVersion;
38+
var browser = {};
39+
40+
browser.mozilla = /mozilla/.test(userAgent.toLowerCase()) && !/webkit/.test(userAgent.toLowerCase());
41+
browser.webkit = /webkit/.test(userAgent.toLowerCase());
42+
browser.opera = /opera/.test(userAgent.toLowerCase());
43+
browser.msie = /msie/.test(userAgent.toLowerCase());
44+
45+
// check if browser is webkit...
46+
if (browser.webkit) {
47+
if(o.add_class_to_body){
48+
elements.body.addClass('browser-webkit');
49+
}
50+
51+
// check if it's chrome, safari, or an iOS browser
52+
if (userAgent.match(/iPad/i) !== null) {
53+
elements.body.addClass('browser-ipad os-ios');
54+
if (window.navigator.standalone) {
55+
elements.body.addClass('ios-webapp');
56+
}
57+
if(o.orientation_listen){
58+
listenToOrientationChange();
59+
}
60+
} else if(userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
61+
elements.body.addClass('browser-iphone os-ios');
62+
if (window.navigator.standalone) {
63+
elements.body.addClass('ios-webapp');
64+
}
65+
if(o.orientation_listen){
66+
listenToOrientationChange();
67+
}
68+
} else if(userAgent.match(/Chrome/i)) {
69+
elements.body.addClass('browser-chrome');
70+
} else if(userAgent.match(/Safari/i)) {
71+
elements.body.addClass('browser-safari');
72+
}
73+
74+
// ...if browser is NOT webkit, run through the other browsers
75+
} else {
76+
if (browser.msie) {
77+
elements.body.addClass('browser-ie');
78+
} else if (browser.mozilla) {
79+
elements.body.addClass('browser-firefox');
80+
} else if (browser.opera) {
81+
elements.body.addClass('browser-opera');
82+
}
83+
}
84+
85+
// detect operating systems
86+
if (appVersion.indexOf("Mac")!=-1) {
87+
elements.body.addClass('os-mac');
88+
} else if (appVersion.indexOf("Win")!=-1) {
89+
elements.body.addClass('os-win');
90+
} else if (appVersion.indexOf("X11")!=-1) {
91+
elements.body.addClass('os-unix');
92+
} else if (appVersion.indexOf("Linux")!=-1) {
93+
elements.body.addClass('os-linux');
94+
} else {
95+
elements.body.addClass('os-unknown');
96+
}
97+
98+
// detect touch
99+
if (o.detect_touch) {
100+
if (!!('ontouchstart' in window) || !!('onmsgesturechange' in window)) {
101+
elements.body.addClass('feature-touch');
102+
}
103+
}
104+
105+
106+
}
107+
108+
109+
function listenToOrientationChange() {
110+
111+
function changed(orientation) {
112+
if (orientation === 0){
113+
elements.body.removeClass('portrait landscape').addClass('portrait');
114+
} else if (orientation === 180){
115+
elements.body.removeClass('portrait landscape').addClass('portrait');
116+
} else if (orientation === 90){
117+
elements.body.removeClass('portrait landscape').addClass('landscape');
118+
} else if (orientation === -90){
119+
elements.body.removeClass('portrait landscape').addClass('landscape');
120+
}
121+
}
122+
123+
changed(window.orientation);
124+
125+
window.onorientationchange = function(){
126+
var orientation = window.orientation;
127+
changed(orientation);
128+
};
129+
}
130+
131+
init();
132+
}
133+
134+
NI.BrowserDetection = BrowserDetection;
135+
136+
return BrowserDetection;
137+
138+
});

0 commit comments

Comments
 (0)