Javascript : Detect browser using Javascript

var isOpera = !!window.opera || navigator.userAgent.indexOf(‘ OPR/’) >= 0;
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
var isFirefox = typeof InstallTrigger !== ‘undefined’;   // Firefox 1.0+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf(‘Constructor’) > 0;
// At least Safari 3+: “[object HTMLElementConstructor]”
var isChrome = !!window.chrome && !isOpera;              // Chrome 1+
var isIE = /@cc_on!@/false || !!document.documentMode;   // At least IE6

var output = ‘Detecting browsers by ducktyping:<hr>’;
output += ‘isFirefox: ‘ + isFirefox + ‘<br>’;
output += ‘isChrome: ‘ + isChrome + ‘<br>’;
output += ‘isSafari: ‘ + isSafari + ‘<br>’;
output += ‘isOpera: ‘ + isOpera + ‘<br>’;
output += ‘isIE: ‘ + isIE + ‘<br>’;
document.body.innerHTML = output;

Javascript:TypeError: $(…).live is not a function

jQuery .live() has been removed in version 1.9 onwards.

That means if you are upgrading from version 1.8 and earlier, you will notice things breaking if you do not follow the migration guide below. You must not simply replace .live() with .on()!


Read before you start doing a search and replace:

For quick/hot fixes on a live site, do not just replace the keyword live with on,
as the parameters are different!

.live(events, function)

should map to:

.on(eventType, selector, function)

The selector is very important! If you do not need to use this for any reason, set it to null.
Continue reading

Javascript : Array Sort By Query

function sortArray(matches, query){
var new_array = new Array();
var old_array = new Array();
query = query.toLowerCase();
for(var i=0; i<matches.length; i++){
var string = matches[i].value;
var string_lwr = string.toLowerCase();
var index_string = string_lwr.indexOf(query);
if( index_string == 0){
new_array.push(matches[i]);
} else{
old_array.push(matches[i]);
}
}
return new_array.concat(old_array);
}