Browser check with a different way
December 31, 2006 23:16
While the www technologies going smarter, much more easier, development techniques too goes smarter, smaller, bright&brilliant in a mathematical way. Day after day, we forgetting the old-style development techniques replacing with the new ways.
Although huge percentage of developers don’t need browser check, sometimes it requires to be used. Then I think that this way of browser checking could help some developers about detecting the browsers.
The Script
This script uses Mike Brittain’s inArray prototype script.
<script type="text/javascript">
var is = {
ua: navigator.userAgent.toLowerCase(),
tArray: [],
fArray: [],
browser: function (trueArray, falseArray) {
this.tArray.length=0;
this.fArray.length=0;
for (var i=0; i<truearray .length; i++) {
this.ua.search(trueArray[i])!=-1 ? this.tArray.push(1) : this.tArray.push(0);
}
for (var j=0; j<falseArray.length; j++) {
this.ua.search(falseArray[j])==-1 ? this.fArray.push(0) : this.fArray.push(1);
}
return ((this.tArray.inArray(0) ? 0 : 1) && (this.fArray.inArray(0) ? 1 : 0));
},
debug:function(){
return this.ua;
}
}
/* http://code.mikebrittain.com/2006/01/inarray-method-for-javascript-and-actionscript/ */
Array.prototype.inArray =function(value){
var i;
for(i=0;i<this.length;i++){
if(this[i]===value) return true;
}
return false;
};
</script>
Usage
General usage looks likes:
is.browser([true Conditions], [false Conditions]);
Firefox 2.0 check
if(is.browser(['firefox', 'gecko', 'firefox/2.0'] , ['windowsszz', 'webtv'])) {
alert(’firefox 2.0′)
}
Internet Explorer check (any)
// Because Internet Explorer doesn't run on linux
// Same way (is.browser(['safari'] , ['linux'])) will select all safari versions
if(is.browser(['msie'] , ['linux'])) {
alert(’any msie’);
}
Selecting Opera 7 or 8
// 'sdfg' figures any wrong string, because Opera runs on linux, windows and mac
if(is.browser(['opera', '7.0'] , ['sdfg']) || is.browser(['opera', '8.0'] , ['sdfg'])) {
alert(’Opera 7 or 8′);
}
if you don’t sure about browser specific keywords, use “alert(is.debug());” and use keywords from the debug string for true conditions
alert(is.debug());
Note:Please report bugs and new recommendations


Comments
No comments.
RSS feed for comments on this post. TrackBack URI
Add Comment