Tip #13: Did you know... How to get the browser agent using ASP.NET Ajax ?

When writing JavaScript for a web application, there may be times when you want to do something different depending on the browser that is requesting the page. ASP.NET Ajax has an easy way to this with the Sys.Browser object.

For example, let's say you want to do something different if the requesting browser is Opera. The code snippet below shows how to do that:

 <script language="javascript" type="text/javascript">
function DoSomething() {
    if(Sys.Browser.agent === Sys.Browser.Opera)
    {
        // opera specific code here
    }
}
</script>

There are other useful properties of the Browser object. For example, Sys.Browser.version.toString(); will return the version of the browser, if you need further granularity when deciding what to do for each browser version.

Federico Silva Armas

SDET, ASP.NET QA Team