[ASP.NET AJAX Client] Browser Detection

ASP.NET AJAX Library does a good job to hide all browser compatibilities issues from you. You can just use the library classes without worring about them. But in some cases, you might still need to do some browser specific tasks and figuring out the user’s browser information will be tedious and sometimes error-prone.

The “Sys.Browser” class helps you to detect the browser very easily.  

1.  Sys.Browser Class

The “Sys.Browser” Class provides information about the current Web browser.

2. Properties

The “Sys.Browser” class provides the following data fields:

  • agent: gets an object that represents the user agent of the browser
  • documentMode: the document compatibility mode of the browser
  • hasDebuggerStatement: true if the browser supports debug statements
  • name: the name of the browser as a string
  • version: the version number of the browser

You can access the property like this  “Sys.Browser.propertyname

3. Agents

The following agents are available.

  • Sys.Browser.InternetExplorer
  • Sys.Browser.Firefox
  • Sys.Browser.Safari
  • Sys.Browser.Opera

If the agent cannot be identified, null is returned.

4. Document Modes

The document mode is used for MS IE.

  • 0: any browser other than Internet Explorer
  • 5: compatible with Internet Explorer 5.0
  • 7: compatible with Internet Explorer 7.0
  • 8: compatible with Internet Explorer 8.0

5. Example

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div id="resultArea">
</div>
<script type="text/javascript">
function pageLoad() {
  var sb = new Sys.StringBuilder();
  sb.append('Browser name: ' + Sys.Browser.name + '<br />');
  sb.append('Browser version: ' + Sys.Browser.version + '<br />');
  sb.append('Browser agent: ');
  switch (Sys.Browser.agent) {
    case Sys.Browser.InternetExplorer: sb.append('InternetExplorer<br />'); break;
    case Sys.Browser.Firefox: sb.append('Firefox<br />'); break;
    case Sys.Browser.Safari: sb.append('Safari<br />'); break;
    case Sys.Browser.Opera: sb.append('Opera<br />'); break;
    default: sb.append('N/A<br />'); break;
  }
  sb.append('Browser Document Mode: ' + Sys.Browser.documentMode + '<br />');
  $get('resultArea').innerHTML = sb.toString();
}
</script>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s