Skip to content

Commit cd6e524

Browse files
committed
browser detection for jquery1.9
1 parent db20ca3 commit cd6e524

File tree

1 file changed

+53
-71
lines changed

1 file changed

+53
-71
lines changed

lib/BrowserDetection.js

Lines changed: 53 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,121 +11,103 @@
1111
\E5.,E5F EE1. /; ``*4EEEZhw._ EEEL
1212
```` `` JEEE. `"45EEEhw.,,,]
1313
14-
From 2010 till
14+
From 2010 till infinity
1515
typecode-js v 0.1
1616
*/
1717

1818
define(['jquery', 'NIseed'], function($) {
19-
19+
2020
var window = this,
2121
NI = window.NI;
2222

2323
function BrowserDetection(options) {
2424
var o, elements;
25-
25+
2626
o = $.extend({
27-
addClassToBody:true,
28-
orientationListen:false,
29-
detectWebApp:true
27+
orientation_listen: false,
28+
detect_touch: true
3029
}, options);
31-
30+
3231
elements = {
3332
body:$('body')
3433
};
35-
34+
3635
function init(){
3736
var userAgent = navigator.userAgent;
3837
var appVersion = navigator.appVersion;
39-
var browser = $.browser;
40-
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+
4145
// check if browser is webkit...
4246
if (browser.webkit) {
43-
if(o.addClassToBody){
47+
if(o.add_class_to_body){
4448
elements.body.addClass('browser-webkit');
4549
}
46-
50+
4751
// check if it's chrome, safari, or an iOS browser
4852
if (userAgent.match(/iPad/i) !== null) {
49-
if(o.addClassToBody){
50-
elements.body.addClass('browser-ipad os-ios');
53+
elements.body.addClass('browser-ipad os-ios');
54+
if (window.navigator.standalone) {
55+
elements.body.addClass('ios-webapp');
5156
}
52-
if(o.orientationListen){
57+
if(o.orientation_listen){
5358
listenToOrientationChange();
5459
}
55-
if(o.detectWebApp){
56-
if (window.navigator.standalone) {
57-
elements.body.addClass('ios-webapp');
58-
}
59-
}
6060
} else if(userAgent.match(/iPhone/i) || userAgent.match(/iPod/i)) {
61-
if(o.addClassToBody){
62-
elements.body.addClass('browser-iphone os-ios');
61+
elements.body.addClass('browser-iphone os-ios');
62+
if (window.navigator.standalone) {
63+
elements.body.addClass('ios-webapp');
6364
}
64-
if(o.orientationListen){
65+
if(o.orientation_listen){
6566
listenToOrientationChange();
6667
}
67-
if(o.detectWebApp){
68-
if (window.navigator.standalone) {
69-
elements.body.addClass('ios-webapp');
70-
}
71-
}
7268
} else if(userAgent.match(/Chrome/i)) {
73-
if(o.addClassToBody){
74-
elements.body.addClass('browser-chrome');
75-
}
69+
elements.body.addClass('browser-chrome');
7670
} else if(userAgent.match(/Safari/i)) {
77-
if(o.addClassToBody){
78-
elements.body.addClass('browser-safari');
79-
}
71+
elements.body.addClass('browser-safari');
8072
}
81-
73+
8274
// ...if browser is NOT webkit, run through the other browsers
8375
} else {
8476
if (browser.msie) {
85-
if(o.addClassToBody){
86-
elements.body.addClass('browser-ie');
87-
}
77+
elements.body.addClass('browser-ie');
8878
} else if (browser.mozilla) {
89-
if(o.addClassToBody){
90-
elements.body.addClass('browser-firefox');
91-
}
79+
elements.body.addClass('browser-firefox');
9280
} else if (browser.opera) {
93-
if(o.addClassToBody){
94-
elements.body.addClass('browser-opera');
95-
}
81+
elements.body.addClass('browser-opera');
9682
}
9783
}
9884

99-
100-
// now, detect operating systems
85+
// detect operating systems
10186
if (appVersion.indexOf("Mac")!=-1) {
102-
if(o.addClassToBody){
103-
elements.body.addClass('os-mac');
104-
}
87+
elements.body.addClass('os-mac');
10588
} else if (appVersion.indexOf("Win")!=-1) {
106-
if(o.addClassToBody){
107-
elements.body.addClass('os-win');
108-
}
89+
elements.body.addClass('os-win');
10990
} else if (appVersion.indexOf("X11")!=-1) {
110-
if(o.addClassToBody){
111-
elements.body.addClass('os-unix');
112-
}
91+
elements.body.addClass('os-unix');
11392
} else if (appVersion.indexOf("Linux")!=-1) {
114-
if(o.addClassToBody){
115-
elements.body.addClass('os-linux');
116-
}
93+
elements.body.addClass('os-linux');
11794
} else {
118-
if(o.addClassToBody){
119-
elements.body.addClass('os-unknown');
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');
120102
}
121103
}
122-
104+
105+
123106
}
124-
125-
126-
107+
108+
127109
function listenToOrientationChange() {
128-
110+
129111
function changed(orientation) {
130112
if (orientation === 0){
131113
elements.body.removeClass('portrait landscape').addClass('portrait');
@@ -137,20 +119,20 @@ define(['jquery', 'NIseed'], function($) {
137119
elements.body.removeClass('portrait landscape').addClass('landscape');
138120
}
139121
}
140-
122+
141123
changed(window.orientation);
142-
124+
143125
window.onorientationchange = function(){
144126
var orientation = window.orientation;
145127
changed(orientation);
146128
};
147129
}
148-
130+
149131
init();
150132
}
151133

152-
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
153-
154134
NI.BrowserDetection = BrowserDetection;
155-
135+
136+
return BrowserDetection;
137+
156138
});

0 commit comments

Comments
 (0)