Last Updated: February 25, 2016
·
440
· lucidanonymity

FindBrowser

I wrote this little bit of code last night to help with browser detection, it forces Chrome to report as only Chrome when using userAgent for detection.

http://cdpn.io/JroCs

function FindBrowser(){
/*FindBrowser:
* Requires - jQuery 
* I developed this bit of code to help with browser detection in jquery, I was having trouble
* differentiating Chrome From Safari, this little function helps with that by forcing $.browser['safari']
* to report as false if chrome is found in the userAgent definition
*/
var search_str = ['chrome','firefox','safari','msie'];
var arraylen = search_str.length;
var i = 0;
while (i < arraylen){

    var str = search_str[i];
    var strlen = str.length;
    var browserDetails = navigator.userAgent.toLowerCase();
    var _search = browserDetails.search(str);
    var ret_str = browserDetails.substr(_search,strlen);

        if (ret_str === str){
            console.log('Succesfully found browser -->  '+ ret_str);
            //Here we will force chrome to report as only chrome to jQuery
            if (str === 'chrome'){
                $.browser['safari'] = false;
                return;
            }
        }
        i++;
    }
}