Skip to content
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
.idea/
18 changes: 14 additions & 4 deletions README.mkdn
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,41 @@
CSS Browser Selector is a very small javascript which empowers CSS selectors.
<br />You can now write code for: browser, browser version, platform, platform version, device, device version.
<br />Best part: no more hacks; all compliant code.
<br />More info: [http://rafael.adm.br/css_browser_selector](http://rafael.adm.br/css_browser_selector)
<br />beta/experimental versions: [https://github.com/verbatim/css_browser_selector/](https://github.com/verbatim/css_browser_selector/)

Identifies
<br />browsers: Firefox; IE; Opera; Safari; Chrome, Konqueror, Iron
<br />browser versions: (most importantly: ie6, ie7, ie8, ie9)
<br />browser versions: (most importantly: ie6, ie7, ie8, ie9, ie10, ie11)
<br />rendering engines: Webkit; Mozilla; Gecko
<br />platforms/OSes: Mac; Win: Win8, Win7, Vista, WinXP, Win2k, WinNT; FreeBSD; Linux/x11
<br />platforms/OSes: Mac; Win: Win8.1, Win8, Win7, Vista, WinXP, Win2k, WinNT; FreeBSD; Linux/x11
<br />devices: Ipod; Ipad; Iphone; WebTV; Blackberry; Android; J2me; RIM Playbook; mobile (generic)
<br />enabled technology: JS (use in conjunction with &lt;html class="no-js"> for even more granular control)
<br />language detection

Recent contributors to 0.5, 0.6:
Recent contributors to 0.5, 0.6, 0.7:

more detailed IE detection:
<br />https://github.com/kevingessner/css_browser_selector/

modernized IE detection:
https://github.com/isarmstrong/css_browser_selector

more detailed WIN detection:
<br />https://github.com/saar/css_browser_selector

updated WIN detection
https://github.com/isarmstrong/css_browser_selector

no-js to js:
<br />paul irish: http://paulirish.com/2009/avoiding-the-fouc-v3/

mac versioning
<br />https://github.com/haraldmartin/css_browser_selector

v0.7 2014-12-3
<br />Modern IE detection
<br />Support for Windows 8.1
<br />Added an in-project demo page

v0.6.1 2012-03-14
<br />iOS version detection
Expand Down Expand Up @@ -148,3 +157,4 @@ Contributors:
<br />Alex Wiltschko
<br />Chris Warren and Tony Nelson (http://www.imagetrend.com)
<br />glasser
<br />Ian Armstrong (https://github.com/isarmstrong/)
17 changes: 10 additions & 7 deletions css_browser_selector.js

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

91 changes: 47 additions & 44 deletions css_browser_selector_dev.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
CSS Browser Selector 0.6.1
CSS Browser Selector (revived) 0.7
Originally written by Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
Modified by Ian Armstrong (http://imperativeideas.com)
License: http://creativecommons.org/licenses/by/2.5/

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

Forked Repo:
https://github.com/isarmstrong/css_browser_selector
*/

showLog=true;
Expand All @@ -19,11 +19,11 @@ function css_browser_selector(u)
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;
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',
Expand All @@ -38,25 +38,29 @@ function css_browser_selector(u)
dv='device_',
html=document.documentElement,
b= [

// browser
(!(/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+))(\.\d+)*)/.test(ua)?' '+f+RegExp.$2 + ' '+f+RegExp.$2+"_"+RegExp.$4:'')
(!(/opera|webtv/i.test(ua))&&/msie\s(\d+)/.test(ua))?('ie ie'+(/trident\/7\.0/.test(ua) ? '11' : RegExp.$1))
:is (!(/opera|webtv/i.test(ua))&&/msie\s(\d+)/.test(ua))?('ie ie'+(/trident\/6\.0/.test(ua) ? '10' : RegExp.$1))
:is (!(/opera|webtv/i.test(ua))&&/msie\s(\d+)/.test(ua))?('ie ie'+(/trident\/5\.0/.test(ua) ? '9' : RegExp.$1))
:is (!(/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+))(\.\d+)*)/.test(ua)?' '+f+RegExp.$2 + ' '+f+RegExp.$2+"_"+RegExp.$4:'')
:is('chrome')?w+ ' '+c+(/chrome\/((\d+)(\.(\d+))(\.\d+)*)/.test(ua)?' '+c+RegExp.$2 +((RegExp.$4>0) ? ' '+c+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 +

: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)
: (/Blackberry ?(([0-9]+)([a-z]?))[\/|;]/gi.test(ua)
? ' ' +bb+RegExp.$2 + (RegExp.$3?' ' +bb+RegExp.$2+RegExp.$3:'')
: '')
)
) // blackberry
:is('android') ?

:is('android') ?
( a +
( /Version\/(\d+)(\.(\d+))+/i.test(ua)
? " " + a+ RegExp.$1 + " "+a+ RegExp.$1+RegExp.$2.replace('.','_')
Expand All @@ -65,36 +69,34 @@ function css_browser_selector(u)
? ' '+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 +

: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'
: ( / 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
) //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')?
(
:is('ipad|ipod|iphone')?
(
(
/CPU( iPhone)? OS (\d+[_|\.]\d+([_|\.]\d+)*)/i.test(ua) ?
'ios' + version('ios',RegExp.$2) : ''
Expand All @@ -107,28 +109,29 @@ function css_browser_selector(u)
: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.3')?' win8.1'
: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.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

function screenSize()
function screenSize()
{
var w = window.outerWidth || html.clientWidth;
var h = window.outerHeight || html.clientHeight;
Expand All @@ -141,16 +144,16 @@ function css_browser_selector(u)
html.className = ( html.className +widthClasses );
return widthClasses;
} // screenSize

window.onresize = screenSize;
screenSize();
screenSize();

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);


105 changes: 105 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Browser Selector Test</title>
<style>
body {
background: rgb(220,220,220);
font: normal normal 1em/1.2em Helvetica,Arial,sans-serif;
}
.wrap {
max-width: 960px;
margin: 0 auto;
padding: 15px;
background: rgb(250,250,250);
}
li.ie, .ie .detected { background-color: yellow; }
li.ie6, .ie6 .detected { background-color: #8a6200 }
li.ie7, .ie7 .detected { background-color: #9c6d00 }
li.ie8, .ie8 .detected { background-color: #b78100 }
li.ie9, .ie9 .detected { background-color: #c68800 }
li.ie10, .ie10 .detected { background-color: #dd9e00 }
li.ie11, .ie11 .detected { background-color: #ffb935 }
li.gecko, .gecko .detected { background-color: #929292; }
li.win.gecko, .win.gecko .detected { background-color: #ff3b49; }
li.linux, .linux.gecko .detected { background-color: pink; }
li.opera, .opera .detected { background-color: #28a324; }
li.konqueror, .konqueror .detected { background-color: #5760ff; }
li.webkit, .webkit .detected { background-color: rgb(149, 61, 245); }
li.chrome, .chrome .detected { background-color: #74ffbe; }
.detected {
display: inline-block;
padding: 2px 10px;
font-size: 80%;
}
.legend {
display: inline-block;
padding-left: 20px;
list-style: none;
}
.legend li {
padding: 2px 10px;
}
</style>
</head>
<body>

<div class="wrap">
<h1>Information on this browser</h1>

<div style="padding-left: 20px;"><p class="detected">USER AGENT STRING:<br /><span id="UserAgent"></span></p></div>

<div>
<ul class="legend">
<li style="border-bottom: 1px solid black;"><strong>Legend</strong></li>
<li class="ie">IE</li>
<li class="ie6">IE6</li>
<li class="ie7">IE7</li>
<li class="ie8">IE8</li>
<li class="ie9">IE9 (Max for WinXP)</li>
<li class="ie10">IE10</li>
<li class="ie11">IE11</li>
<li class="gecko">Gecko</li>
<li class="win gecko">Windows Gecko</li>
<li class="linux gecko">Linux Gecko</li>
<li class="opera">Opera Gecko</li>
<li class="konqueror">Konqueror</li>
<li class="webkit">Webkit</li>
<li class="chrome">Chrome</li>
</ul>
</div>

<div>
<ul id="classy" style="display:inline-block;">
<li style="list-style: none; border-bottom: 1px solid black;"><h4 style="margin:0;">List of classes applied</h4></li>
</ul>
</div>
</div>


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="css_browser_selector.js"></script>

<script>
var classList = document.documentElement.className.split(/\s+/);
var items = document.getElementById("classy");

function writeClasses() {
for (var i = 0; i < classList.length; i++) {
var item = document.createElement("li");
item.innerHTML = classList[i];
items.appendChild(item);
}
}

jQuery(function($) {
$('#UserAgent').append(navigator.userAgent);
});

writeClasses();

</script>

</body>
</html>