Detect Browser Dynamics CRM 2011 Rollup 12 the Microsoft way

Paul Nieuwelaar, 05 February 2013

With the introduction of cross-browser support for Dynamics CRM 2011 in rollup 12, developers are now faced with having to make their custom JavaScript compatible with all browsers that users might be using. There are several different ways to detect a user’s browser with JavaScript, but I wanted to see how Microsoft were doing it.

While I was debugging some code, I actually noticed the function ‘Mscrm.Utilities.isFirefox()’ being called within some of CRM’s JavaScript. I did a bit of further investigating and found a whole range of functions used by Microsoft to detect a user’s browser.

 Detect Browser Dynamics CRM 2011 Rollup 12 the Microsoft way

These are all the JavaScript functions I could find that are being used by Microsoft to detect a user’s browser. I’ve included the entire functions instead of just the function names in case you need to customize any of it or create your own functions. It’s technically not supported to use these, as the names could change at any time; so if you want to be safe you can create your own functions:

Mscrm.Utilities.isFirefox = function () {
    return Sys.Browser.agent === Sys.Browser.Firefox
};
Mscrm.Utilities.isIosDevice = function () {
    var uaString = navigator.userAgent;
    if (IsNull(uaString)) return false;
    uaString = uaString.toLowerCase();
    return uaString != "" && uaString.search("ipad|ipod|iphone") > -1
};
Mscrm.Utilities.isIE = function () {
    return Sys.Browser.agent === Sys.Browser.InternetExplorer
};
Mscrm.Utilities.isChrome = function () {
    if (navigator.userAgent.toLowerCase().indexOf("chrome") > -1) return true;
    else return false
};
Mscrm.Utilities.isIE7 = function () {
    return Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version === 7 && !window.document.documentMode
};
Mscrm.Utilities.isIE8 = function () {
    return Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version === 8
};
Mscrm.Utilities.isIE9 = function () {
    return Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version === 9
};
Mscrm.Utilities.isIE10 = function () {
    return Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version === 10
};
Mscrm.Utilities.isIE7OrIE7CompactMode = function () {
    return window.navigator.userAgent.indexOf("MSIE 7") > -1
};

Sys.Browser.agent === Sys.Browser.Safari //this one doesn't have its own function, but is used at least once by Microsoft  

This is a sample of using the functions:

function detectBrowser() {
    if (Mscrm.Utilities.isIE()) {
        alert("This is IE");
    }
    if (Mscrm.Utilities.isFirefox()) {
        alert("This is Firefox");
    }
    if (Mscrm.Utilities.isChrome()) {
        alert("This is Chrome");
    }
} 

Enjoy!