Skip to content

Added support to detect Internet Explorer 11 after Microsoft changes the... #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 157 additions & 48 deletions css_browser_selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,164 @@ identifies
devices: Ipod; Ipad; Iphone; WebTV; Blackberry; Android; J2me; mobile(generic)
enabled technology: JS

v0.6.3 2014-03-06
@silasrm <[email protected]>
- Added support to IE11
@see http://msdn.microsoft.com/en-us/library/ie/hh869301(v=vs.85).aspx
@see http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx
*/

function css_browser_selector(u)
{
var ua=u.toLowerCase(),
is=function(t) { return ua.indexOf(t)>-1},
g='gecko',
w='webkit',
s='safari',
o='opera',
m='mobile',
f='firefox',
h=document.documentElement,
b= [
/* hat tip: https://github.com/kevingessner/css_browser_selector/ */
(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+(/trident\/4\.0/.test(ua) ? '8' : RegExp.$1))
:is('firefox/')?g+" "+f+(/firefox\/(\d+(\.?\d+)*)/.test(ua)?' '+f+RegExp.$1.replace(/\./g,"").substr(0,2):'')
:is('gecko/')?g
: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:''))
:is('konqueror')?'konqueror'
:is('blackberry')?m+' blackberry'
:is('android')?m+' android'
:is('chrome')?w+' chrome'
:is('iron')?w+' iron'
:is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:'')
:is('mozilla/')?g:''
,is('j2me')?m+' j2me'
:is('iphone')?m+' iphone'
:is('ipod')?m+' ipod'
:is('ipad')?m+' ipad'
:is('mac')?'mac'
:is('darwin')?'mac'
:is('webtv')?'webtv'
/* hat tip: https://github.com/saar/css_browser_selector */
:is('win')?'win'+
(is('windows nt 6.2')?' win8'
:is('windows nt 6.1')?' win7'
:is('windows nt 6.0')?' vista'
:is('windows nt 5.2') || is('windows nt 5.1') ? ' xp'
:is('windows nt 5.0')?' win2k': ''
)
:is('freebsd')?'freebsd'
:(is('x11')||is('linux'))?'linux':''
,'js'
];
var c = b.join(' ');
/* hat tip, paul irish: http://paulirish.com/2009/avoiding-the-fouc-v3/ */
h.className = ( h.className.replace(/no-?js/g,"") + " " + c ).replace(/^ /, "");
return c;
}
showLog=true;
function log(m) {if ( window.console && showLog ) {console.log(m); } }

function css_browser_selector(u) {
var uaInfo = {},
screens = [320, 480, 640, 768, 1024, 1152, 1280, 1440, 1680, 1920, 2560],
allScreens = screens.length,
ua=u.toLowerCase(),
is=function(t) { return RegExp(t,"i").test(ua); },
version = function(p,n)
{
n=n.replace(".","_"); var i = n.indexOf('_'), ver="";
while (i>0) {ver += " "+ p+n.substring(0,i);i = n.indexOf('_', i+1);}
ver += " "+p+n; return ver;
},
g='gecko',
w='webkit',
c='chrome',
f='firefox',
s='safari',
o='opera',
m='mobile',
a='android',
bb='blackberry',
lang='lang_',
dv='device_',
html=document.documentElement,
b= [

// browser
((!(/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))
:is('firefox/')?g+ " " + f+(/firefox\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)?' '+f+RegExp.$2 + ' '+f+RegExp.$2+"_"+RegExp.$4:'')
:is('gecko/')?g
: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:''))
:is('konqueror')?'konqueror'

:is('blackberry') ?
( bb +
( /Version\/(\d+)(\.(\d+)+)/i.test(ua)
? " " + bb+ RegExp.$1 + " "+bb+ RegExp.$1+RegExp.$2.replace('.','_')
: (/Blackberry ?(([0-9]+)([a-z]?))[\/|;]/gi.test(ua)
? ' ' +bb+RegExp.$2 + (RegExp.$3?' ' +bb+RegExp.$2+RegExp.$3:'')
: '')
)
) // blackberry

:is('android') ?
( a +
( /Version\/(\d+)(\.(\d+))+/i.test(ua)
? " " + a+ RegExp.$1 + " "+a+ RegExp.$1+RegExp.$2.replace('.','_')
: '')
+ (/Android (.+); (.+) Build/i.test(ua)
? ' '+dv+( (RegExp.$2).replace(/ /g,"_") ).replace(/-/g,"_")
:'' )
) //android

:is('chrome')?w+ ' '+c+(/chrome\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)?' '+c+RegExp.$2 +((RegExp.$4>0) ? ' '+c+RegExp.$2+"_"+RegExp.$4:''):'')

:is('iron')?w+' iron'

:is('applewebkit/') ?
( w+ ' '+ s +
( /version\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)
? ' '+ s +RegExp.$2 + " "+s+ RegExp.$2+RegExp.$3.replace('.','_')
: ( / Safari\/(\d+)/i.test(ua)
?
( (RegExp.$1=="419" || RegExp.$1=="417" || RegExp.$1=="416" || RegExp.$1=="412" ) ? ' '+ s + '2_0'
: RegExp.$1=="312" ? ' '+ s + '1_3'
: RegExp.$1=="125" ? ' '+ s + '1_2'
: RegExp.$1=="85" ? ' '+ s + '1_0'
: '' )
:'')
)
) //applewebkit

:is('mozilla/')?g
:''

// mobile
,is("android|mobi|mobile|j2me|iphone|ipod|ipad|blackberry|playbook|kindle|silk")?m:''

// os/platform
,is('j2me')?'j2me'
:is('ipad|ipod|iphone')?
(
(
/CPU( iPhone)? OS (\d+[_|\.]\d+([_|\.]\d+)*)/i.test(ua) ?
'ios' + version('ios',RegExp.$2) : ''
) + ' ' + ( /(ip(ad|od|hone))/gi.test(ua) ? RegExp.$1 : "" )
) //'iphone'
//:is('ipod')?'ipod'
//:is('ipad')?'ipad'
:is('playbook')?'playbook'
:is('kindle|silk')?'kindle'
:is('playbook')?'playbook'
:is('mac')?'mac'+ (/mac os x ((\d+)[.|_](\d+))/.test(ua) ? ( ' mac' + (RegExp.$2) + ' mac' + (RegExp.$1).replace('.',"_") ) : '' )
:is('win')?'win'+
(is('windows nt 6.2')?' win8'
:is('windows nt 6.1')?' win7'
:is('windows nt 6.0')?' vista'
:is('windows nt 5.2') || is('windows nt 5.1') ? ' win_xp'
:is('windows nt 5.0')?' win_2k'
:is('windows nt 4.0') || is('WinNT4.0') ?' win_nt'
: ''
)
:is('freebsd')?'freebsd'
:(is('x11|linux'))?'linux'
:''

// user agent language
,(/[; |\[](([a-z]{2})(\-[a-z]{2})?)[)|;|\]]/i.test(ua))?(lang+RegExp.$2).replace("-","_")+(RegExp.$3!=''?(' '+lang+RegExp.$1).replace("-","_"):''):''

// beta: test if running iPad app
,( is('ipad|iphone|ipod') && !is('safari') ) ? 'ipad_app' : ''


]; // b

console.debug(ua);

function screenSize() {
var w = window.outerWidth || html.clientWidth;
var h = window.outerHeight || html.clientHeight;
uaInfo.orientation = ((w<h) ? "portrait" : "landscape");
// remove previous min-width, max-width, client-width, client-height, and orientation
html.className = html.className.replace(/ ?orientation_\w+/g, "").replace(/ [min|max|cl]+[w|h]_\d+/g, "")
for (var i=(allScreens-1);i>=0;i--) { if (w >= screens[i] ) { uaInfo.maxw = screens[i]; break; }}
widthClasses="";
for (var info in uaInfo) { widthClasses+=" "+info+"_"+ uaInfo[info] };
html.className = ( html.className +widthClasses );
return widthClasses;
} // screenSize

window.onresize = screenSize;
screenSize();

function retina(){
var r = window.devicePixelRatio > 1;
if (r) {
html.className+=' retina';
}
else {
html.className+=' non-retina';
}
}
retina();

var cssbs = (b.join(' ')) + " js ";
html.className = ( cssbs + html.className.replace(/\b(no[-|_]?)?js\b/g,"") ).replace(/^ /, "").replace(/ +/g," ");

return cssbs;
}

css_browser_selector(navigator.userAgent);
18 changes: 18 additions & 0 deletions css_browser_selector.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions css_browser_selector_dev.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
/*
CSS Browser Selector 0.6.2
CSS Browser Selector 0.6.3
Originally written by Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/

Co-maintained by:
https://github.com/verbatim/css_browser_selector

*/

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

function css_browser_selector(u)
{
function css_browser_selector(u) {
var uaInfo = {},
screens = [320, 480, 640, 768, 1024, 1152, 1280, 1440, 1680, 1920, 2560],
allScreens = screens.length,
Expand All @@ -40,7 +38,7 @@ function css_browser_selector(u)
b= [

// browser
(!(/opera|webtv/i.test(ua))&&/msie\s(\d+)/.test(ua))?('ie ie'+(/trident\/4\.0/.test(ua) ? '8' : RegExp.$1))
((!(/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))
:is('firefox/')?g+ " " + f+(/firefox\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)?' '+f+RegExp.$2 + ' '+f+RegExp.$2+"_"+RegExp.$4:'')
:is('gecko/')?g
: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:''))
Expand Down Expand Up @@ -127,9 +125,10 @@ function css_browser_selector(u)


]; // b

console.debug(ua);

function screenSize()
{
function screenSize() {
var w = window.outerWidth || html.clientWidth;
var h = window.outerHeight || html.clientHeight;
uaInfo.orientation = ((w<h) ? "portrait" : "landscape");
Expand All @@ -140,7 +139,7 @@ function css_browser_selector(u)
for (var info in uaInfo) { widthClasses+=" "+info+"_"+ uaInfo[info] };
html.className = ( html.className +widthClasses );
return widthClasses;
} // screenSize
} // screenSize

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

return cssbs;
}
}

css_browser_selector(navigator.userAgent);


12 changes: 8 additions & 4 deletions tests/test_ie.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

var user_agent_strings = [

["Internet Explorer 11?","header"],

["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"],
["Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko","ie ie11 win win7 js"],
["Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko","ie ie11 win win7 js"],
["Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; Trident/7.0)","ie ie11 win js"],

["Internet Explorer 10?","header"],

["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"],
Expand Down Expand Up @@ -292,7 +299,4 @@
<body>

</body>
</html>



</html>