Skip to content

Commit fe91e90

Browse files
committed
Merge pull request #3 from silasrm/master
Added support to detect Internet Explorer 11 after Microsoft changes the...
2 parents 8a0c980 + a347ceb commit fe91e90

File tree

4 files changed

+191
-63
lines changed

4 files changed

+191
-63
lines changed

css_browser_selector.js

Lines changed: 157 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,55 +23,164 @@ identifies
2323
devices: Ipod; Ipad; Iphone; WebTV; Blackberry; Android; J2me; mobile(generic)
2424
enabled technology: JS
2525
26+
v0.6.3 2014-03-06
27+
28+
- Added support to IE11
29+
@see http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx
30+
@see http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx
2631
*/
2732

28-
function css_browser_selector(u)
29-
{
30-
var ua=u.toLowerCase(),
31-
is=function(t) { return ua.indexOf(t)>-1},
32-
g='gecko',
33-
w='webkit',
34-
s='safari',
35-
o='opera',
36-
m='mobile',
37-
f='firefox',
38-
h=document.documentElement,
39-
b= [
40-
/* hat tip: https://github.com/kevingessner/css_browser_selector/ */
41-
(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+(/trident\/4\.0/.test(ua) ? '8' : RegExp.$1))
42-
:is('firefox/')?g+" "+f+(/firefox\/(\d+(\.?\d+)*)/.test(ua)?' '+f+RegExp.$1.replace(/\./g,"").substr(0,2):'')
43-
:is('gecko/')?g
44-
:is('opera')?o+(/version\/((\d+)(\.\d+)*)/.test(ua)?' '+o+RegExp.$2 + ' '+o+RegExp.$2+(RegExp.$3).replace(".","_").substr(0,2):(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:''))
45-
:is('konqueror')?'konqueror'
46-
:is('blackberry')?m+' blackberry'
47-
:is('android')?m+' android'
48-
:is('chrome')?w+' chrome'
49-
:is('iron')?w+' iron'
50-
:is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:'')
51-
:is('mozilla/')?g:''
52-
,is('j2me')?m+' j2me'
53-
:is('iphone')?m+' iphone'
54-
:is('ipod')?m+' ipod'
55-
:is('ipad')?m+' ipad'
56-
:is('mac')?'mac'
57-
:is('darwin')?'mac'
58-
:is('webtv')?'webtv'
59-
/* hat tip: https://github.com/saar/css_browser_selector */
60-
:is('win')?'win'+
61-
(is('windows nt 6.2')?' win8'
62-
:is('windows nt 6.1')?' win7'
63-
:is('windows nt 6.0')?' vista'
64-
:is('windows nt 5.2') || is('windows nt 5.1') ? ' xp'
65-
:is('windows nt 5.0')?' win2k': ''
66-
)
67-
:is('freebsd')?'freebsd'
68-
:(is('x11')||is('linux'))?'linux':''
69-
,'js'
70-
];
71-
var c = b.join(' ');
72-
/* hat tip, paul irish: http://paulirish.com/2009/avoiding-the-fouc-v3/ */
73-
h.className = ( h.className.replace(/no-?js/g,"") + " " + c ).replace(/^ /, "");
74-
return c;
75-
}
33+
showLog=true;
34+
function log(m) {if ( window.console && showLog ) {console.log(m); } }
35+
36+
function css_browser_selector(u) {
37+
var uaInfo = {},
38+
screens = [320, 480, 640, 768, 1024, 1152, 1280, 1440, 1680, 1920, 2560],
39+
allScreens = screens.length,
40+
ua=u.toLowerCase(),
41+
is=function(t) { return RegExp(t,"i").test(ua); },
42+
version = function(p,n)
43+
{
44+
n=n.replace(".","_"); var i = n.indexOf('_'), ver="";
45+
while (i>0) {ver += " "+ p+n.substring(0,i);i = n.indexOf('_', i+1);}
46+
ver += " "+p+n; return ver;
47+
},
48+
g='gecko',
49+
w='webkit',
50+
c='chrome',
51+
f='firefox',
52+
s='safari',
53+
o='opera',
54+
m='mobile',
55+
a='android',
56+
bb='blackberry',
57+
lang='lang_',
58+
dv='device_',
59+
html=document.documentElement,
60+
b= [
61+
62+
// browser
63+
((!(/opera|webtv/i.test(ua))&&/msie\s(\d+)/.test(ua)||(/trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.test(ua))))?('ie ie'+(/trident\/4\.0/.test(ua) ? '8' : RegExp.$1 == '11.0'?'11':RegExp.$1))
64+
:is('firefox/')?g+ " " + f+(/firefox\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)?' '+f+RegExp.$2 + ' '+f+RegExp.$2+"_"+RegExp.$4:'')
65+
:is('gecko/')?g
66+
:is('opera')?o+(/version\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)?' '+o+RegExp.$2 + ' '+o+RegExp.$2+"_"+RegExp.$4 : (/opera(\s|\/)(\d+)\.(\d+)/.test(ua)?' '+o+RegExp.$2+" "+o+RegExp.$2+"_"+RegExp.$3:''))
67+
:is('konqueror')?'konqueror'
68+
69+
:is('blackberry') ?
70+
( bb +
71+
( /Version\/(\d+)(\.(\d+)+)/i.test(ua)
72+
? " " + bb+ RegExp.$1 + " "+bb+ RegExp.$1+RegExp.$2.replace('.','_')
73+
: (/Blackberry ?(([0-9]+)([a-z]?))[\/|;]/gi.test(ua)
74+
? ' ' +bb+RegExp.$2 + (RegExp.$3?' ' +bb+RegExp.$2+RegExp.$3:'')
75+
: '')
76+
)
77+
) // blackberry
78+
79+
:is('android') ?
80+
( a +
81+
( /Version\/(\d+)(\.(\d+))+/i.test(ua)
82+
? " " + a+ RegExp.$1 + " "+a+ RegExp.$1+RegExp.$2.replace('.','_')
83+
: '')
84+
+ (/Android (.+); (.+) Build/i.test(ua)
85+
? ' '+dv+( (RegExp.$2).replace(/ /g,"_") ).replace(/-/g,"_")
86+
:'' )
87+
) //android
88+
89+
:is('chrome')?w+ ' '+c+(/chrome\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)?' '+c+RegExp.$2 +((RegExp.$4>0) ? ' '+c+RegExp.$2+"_"+RegExp.$4:''):'')
90+
91+
:is('iron')?w+' iron'
92+
93+
:is('applewebkit/') ?
94+
( w+ ' '+ s +
95+
( /version\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)
96+
? ' '+ s +RegExp.$2 + " "+s+ RegExp.$2+RegExp.$3.replace('.','_')
97+
: ( / Safari\/(\d+)/i.test(ua)
98+
?
99+
( (RegExp.$1=="419" || RegExp.$1=="417" || RegExp.$1=="416" || RegExp.$1=="412" ) ? ' '+ s + '2_0'
100+
: RegExp.$1=="312" ? ' '+ s + '1_3'
101+
: RegExp.$1=="125" ? ' '+ s + '1_2'
102+
: RegExp.$1=="85" ? ' '+ s + '1_0'
103+
: '' )
104+
:'')
105+
)
106+
) //applewebkit
107+
108+
:is('mozilla/')?g
109+
:''
110+
111+
// mobile
112+
,is("android|mobi|mobile|j2me|iphone|ipod|ipad|blackberry|playbook|kindle|silk")?m:''
113+
114+
// os/platform
115+
,is('j2me')?'j2me'
116+
:is('ipad|ipod|iphone')?
117+
(
118+
(
119+
/CPU( iPhone)? OS (\d+[_|\.]\d+([_|\.]\d+)*)/i.test(ua) ?
120+
'ios' + version('ios',RegExp.$2) : ''
121+
) + ' ' + ( /(ip(ad|od|hone))/gi.test(ua) ? RegExp.$1 : "" )
122+
) //'iphone'
123+
//:is('ipod')?'ipod'
124+
//:is('ipad')?'ipad'
125+
:is('playbook')?'playbook'
126+
:is('kindle|silk')?'kindle'
127+
:is('playbook')?'playbook'
128+
:is('mac')?'mac'+ (/mac os x ((\d+)[.|_](\d+))/.test(ua) ? ( ' mac' + (RegExp.$2) + ' mac' + (RegExp.$1).replace('.',"_") ) : '' )
129+
:is('win')?'win'+
130+
(is('windows nt 6.2')?' win8'
131+
:is('windows nt 6.1')?' win7'
132+
:is('windows nt 6.0')?' vista'
133+
:is('windows nt 5.2') || is('windows nt 5.1') ? ' win_xp'
134+
:is('windows nt 5.0')?' win_2k'
135+
:is('windows nt 4.0') || is('WinNT4.0') ?' win_nt'
136+
: ''
137+
)
138+
:is('freebsd')?'freebsd'
139+
:(is('x11|linux'))?'linux'
140+
:''
141+
142+
// user agent language
143+
,(/[; |\[](([a-z]{2})(\-[a-z]{2})?)[)|;|\]]/i.test(ua))?(lang+RegExp.$2).replace("-","_")+(RegExp.$3!=''?(' '+lang+RegExp.$1).replace("-","_"):''):''
144+
145+
// beta: test if running iPad app
146+
,( is('ipad|iphone|ipod') && !is('safari') ) ? 'ipad_app' : ''
147+
148+
149+
]; // b
150+
151+
console.debug(ua);
152+
153+
function screenSize() {
154+
var w = window.outerWidth || html.clientWidth;
155+
var h = window.outerHeight || html.clientHeight;
156+
uaInfo.orientation = ((w<h) ? "portrait" : "landscape");
157+
// remove previous min-width, max-width, client-width, client-height, and orientation
158+
html.className = html.className.replace(/ ?orientation_\w+/g, "").replace(/ [min|max|cl]+[w|h]_\d+/g, "")
159+
for (var i=(allScreens-1);i>=0;i--) { if (w >= screens[i] ) { uaInfo.maxw = screens[i]; break; }}
160+
widthClasses="";
161+
for (var info in uaInfo) { widthClasses+=" "+info+"_"+ uaInfo[info] };
162+
html.className = ( html.className +widthClasses );
163+
return widthClasses;
164+
} // screenSize
165+
166+
window.onresize = screenSize;
167+
screenSize();
168+
169+
function retina(){
170+
var r = window.devicePixelRatio > 1;
171+
if (r) {
172+
html.className+=' retina';
173+
}
174+
else {
175+
html.className+=' non-retina';
176+
}
177+
}
178+
retina();
179+
180+
var cssbs = (b.join(' ')) + " js ";
181+
html.className = ( cssbs + html.className.replace(/\b(no[-|_]?)?js\b/g,"") ).replace(/^ /, "").replace(/ +/g," ");
182+
183+
return cssbs;
184+
}
76185

77186
css_browser_selector(navigator.userAgent);

css_browser_selector.min.js

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

css_browser_selector_dev.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
/*
2-
CSS Browser Selector 0.6.2
2+
CSS Browser Selector 0.6.3
33
Originally written by Rafael Lima (http://rafael.adm.br)
44
http://rafael.adm.br/css_browser_selector
55
License: http://creativecommons.org/licenses/by/2.5/
66
77
Co-maintained by:
88
https://github.com/verbatim/css_browser_selector
9-
109
*/
1110

1211
showLog=true;
1312
function log(m) {if ( window.console && showLog ) {console.log(m); } }
1413

15-
function css_browser_selector(u)
16-
{
14+
function css_browser_selector(u) {
1715
var uaInfo = {},
1816
screens = [320, 480, 640, 768, 1024, 1152, 1280, 1440, 1680, 1920, 2560],
1917
allScreens = screens.length,
@@ -40,7 +38,7 @@ function css_browser_selector(u)
4038
b= [
4139

4240
// browser
43-
(!(/opera|webtv/i.test(ua))&&/msie\s(\d+)/.test(ua))?('ie ie'+(/trident\/4\.0/.test(ua) ? '8' : RegExp.$1))
41+
((!(/opera|webtv/i.test(ua))&&/msie\s(\d+)/.test(ua)||(/trident\/.*rv:([0-9]{1,}[\.0-9]{0,})/.test(ua))))?('ie ie'+(/trident\/4\.0/.test(ua) ? '8' : RegExp.$1 == '11.0'?'11':RegExp.$1))
4442
:is('firefox/')?g+ " " + f+(/firefox\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)?' '+f+RegExp.$2 + ' '+f+RegExp.$2+"_"+RegExp.$4:'')
4543
:is('gecko/')?g
4644
:is('opera')?o+(/version\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)?' '+o+RegExp.$2 + ' '+o+RegExp.$2+"_"+RegExp.$4 : (/opera(\s|\/)(\d+)\.(\d+)/.test(ua)?' '+o+RegExp.$2+" "+o+RegExp.$2+"_"+RegExp.$3:''))
@@ -127,9 +125,10 @@ function css_browser_selector(u)
127125

128126

129127
]; // b
128+
129+
console.debug(ua);
130130

131-
function screenSize()
132-
{
131+
function screenSize() {
133132
var w = window.outerWidth || html.clientWidth;
134133
var h = window.outerHeight || html.clientHeight;
135134
uaInfo.orientation = ((w<h) ? "portrait" : "landscape");
@@ -140,7 +139,7 @@ function css_browser_selector(u)
140139
for (var info in uaInfo) { widthClasses+=" "+info+"_"+ uaInfo[info] };
141140
html.className = ( html.className +widthClasses );
142141
return widthClasses;
143-
} // screenSize
142+
} // screenSize
144143

145144
window.onresize = screenSize;
146145
screenSize();
@@ -160,8 +159,6 @@ function css_browser_selector(u)
160159
html.className = ( cssbs + html.className.replace(/\b(no[-|_]?)?js\b/g,"") ).replace(/^ /, "").replace(/ +/g," ");
161160

162161
return cssbs;
163-
}
162+
}
164163

165164
css_browser_selector(navigator.userAgent);
166-
167-

tests/test_ie.html

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
var user_agent_strings = [
1414

15+
["Internet Explorer 11?","header"],
16+
17+
["Mozilla/5.0 (Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; rv:11.0) like Gecko","ie ie11 win win7 js"],
18+
["Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko","ie ie11 win win7 js"],
19+
["Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko","ie ie11 win win7 js"],
20+
["Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0)","ie ie11 win js"],
21+
1522
["Internet Explorer 10?","header"],
1623

1724
["Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0","ie ie10 win win7 js"],
@@ -292,7 +299,4 @@
292299
<body>
293300

294301
</body>
295-
</html>
296-
297-
298-
302+
</html>

0 commit comments

Comments
 (0)