Browser Detection In Asp.Net

Browser Detection In Asp.Net

In this post i'm explaining how to detect browser type, browser name, version, cookies support and javascript version using c# or javascript in asp.net.

Detecting browser using C# is quite simple in asp.net, below mention code can detect all above mentioned features.

protected void Page_Load(object sender, EventArgs e)
    {
        System.Web.HttpBrowserCapabilities browserDetection = Request.Browser;
        lblBrowserType.Text = browserDetection.Type;
        lblBrowserName.Text = browserDetection.Browser;
        lblBrowserVersion.Text = browserDetection.Version;
        
        lblBrowserCookieSupport.Text = browserDetection.Cookies.ToString();
        lblBrowserJavaScriptVersion.Text = browserDetection.EcmaScriptVersion.ToString();

    }


To detect browser through javascript we can write code as mentioned below

   1:  var nVer = navigator.appVersion;
   2:  var nAgt = navigator.userAgent;
   3:  var browserName  = navigator.appName;
   4:  var fullVersion  = ''+parseFloat(navigator.appVersion); 
   5:  var majorVersion = parseInt(navigator.appVersion,10);
   6:  var nameOffset,verOffset,ix;
   7:   
   8:  // In Opera, the true version is after "Opera" or after "Version"
   9:  if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
  10:   browserName = "Opera";
  11:   fullVersion = nAgt.substring(verOffset+6);
  12:   if ((verOffset=nAgt.indexOf("Version"))!=-1) 
  13:     fullVersion = nAgt.substring(verOffset+8);
  14:  }
  15:  // In MSIE, the true version is after "MSIE" in userAgent
  16:  else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
  17:   browserName = "Microsoft Internet Explorer";
  18:   fullVersion = nAgt.substring(verOffset+5);
  19:  }
  20:  // In Chrome, the true version is after "Chrome" 
  21:  else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
  22:   browserName = "Chrome";
  23:   fullVersion = nAgt.substring(verOffset+7);
  24:  }
  25:  // In Safari, the true version is after "Safari" or after "Version" 
  26:  else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
  27:   browserName = "Safari";
  28:   fullVersion = nAgt.substring(verOffset+7);
  29:   if ((verOffset=nAgt.indexOf("Version"))!=-1) 
  30:     fullVersion = nAgt.substring(verOffset+8);
  31:  }
  32:  // In Firefox, the true version is after "Firefox" 
  33:  else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
  34:   browserName = "Firefox";
  35:   fullVersion = nAgt.substring(verOffset+8);
  36:  }
  37:  // In most other browsers, "name/version" is at the end of userAgent 
  38:  else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
  39:            (verOffset=nAgt.lastIndexOf('/')) ) 
  40:  {
  41:   browserName = nAgt.substring(nameOffset,verOffset);
  42:   fullVersion = nAgt.substring(verOffset+1);
  43:   if (browserName.toLowerCase()==browserName.toUpperCase()) {
  44:    browserName = navigator.appName;
  45:   }
  46:  }
  47:  // trim the fullVersion string at semicolon/space if present
  48:  if ((ix=fullVersion.indexOf(";"))!=-1)
  49:     fullVersion=fullVersion.substring(0,ix);
  50:  if ((ix=fullVersion.indexOf(" "))!=-1)
  51:     fullVersion=fullVersion.substring(0,ix);
  52:   
  53:  majorVersion = parseInt(''+fullVersion,10);
  54:  if (isNaN(majorVersion)) {
  55:   fullVersion  = ''+parseFloat(navigator.appVersion); 
  56:   majorVersion = parseInt(navigator.appVersion,10);
  57:  }
  58:   
  59:  document.write(''
  60:   +'Browser name  = '+browserName+'<br>'
  61:   +'Full version  = '+fullVersion+'<br>'
  62:   +'Major version = '+majorVersion+'<br>'
  63:   +'navigator.appName = '+navigator.appName+'<br>'
  64:   +'navigator.userAgent = '+navigator.userAgent+'<br>'
  65:  )


If you like this post than join us or share

0 comments:

Find More Articles