Skip to main content

Get Browser Name and Version in Javascript

          JQuery stopped providing $.browser function after 1.9 release.So you have to write your own code to get know browser and its version.There is not standard procedure or code to achieve this.
But I will show you to get browser name and version of  IE > 6, Google Chrome And FF.

Here we have AGENT to rescue.Every browser has navigation object available in its script.In this object userAgent field contains current browser information.

So you can ask me What is User-Agent?How will it help us?
          As I said earlier it contain browser information.There is not standard to show each information but almost every browser support it.
    There is complicated history behind user-agent variable.But in simple sense we can say that user-agent is used by server side component to get information of client-side browser and its compatibility to page it will going to rendered.So we conclude that this user-agent information is little bit 50% same in all browser.
Why is it(user-agent) 50% same in all browser?
   Simple to cheat the server.Suppose we invented new browser and its user-agent information is different from all,what will happen?
    The server side component gets puzzled and it will think that our browser does not support  functionality.

Here some sample user-agent information.

IE 11 User-agent:
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko 
G Chrome User-agent:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36
FireFox User-agent:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0
IE < 11 User-agent:
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0) 

Microsoft changed its  user agent information pattern from version 11.Here we have added support to all IE including IE 11.

So enough.We we shift to coding part.
In all user agent we have found some similar part and through which we will extract information.

var BrowserSupport = {
  init : function() {
    //get browser name
    this.browser = this.searchBrowserName(navigator.userAgent) || "N/A";
    //get browser version
    this.version = this.searchBrowserVersion(navigator.userAgent)
 || this.searchBrowserVersion(navigator.appVersion)
 || "0";},
 searchBrowserName : function(dataString) {
     var data=this.dataBrowser;
     for ( var i = 0; i < data.length; i++) {
 this.versionSearchString = data[i].versionSearch || data[i].identity;
 if (dataString) {
  //check browser name is present in BROWSER INFO string
    if (dataString.indexOf(data[i].subString) != -1)
   return data[i].identity;
  searchBrowserVersion : function(dataString) {
     var index = dataString.indexOf(this.versionSearchString);
 if (index == -1)
     return parseFloat(dataString.substring(index+this.versionSearchString.length+ 1));
   dataBrowser : [{
   subString : "Chrome",
   identity : "Chrome" //identity is Browser name
  }, {
   subString : "MSIE",
   identity : "Explorer", //identity is Browser name
   versionSearch : "MSIE"
  }, {
   subString : "Firefox",
   identity : "Firefox" //identity is Browser name
   subString : "Trident",//for IE 11
   identity : "Explorer",//identity is Browser name
   versionSearch : "rv" 

You can call function like

console.log(BrowserSupport.browser+" - "+BrowserSupport.version);

Thank you.


Popular posts from this blog

Share data between Iframe and its Parent using JQuery

There could be requirement that you have to pass variables values to Iframe from parent or in other scenario you have to pass values from Iframe to its parent.I am going to show simple example of above situation with the help of JQuery.
           Support we have parent HTML page having iframe included like below

<html> <body> <input id="parentValue" type="text" value="I am from Parent !" /> <iframe src="domainurl/relativepath" id="iframe"></iframe> <script> $(document).ready(function() { //code to take value from child iframe var childValue = $("#iframe").contents().find("#childValue").val(); alert(childValue); }); </script> </body> </html> 
        and we have frame code like below
<html> <body> <input id="childValue" type="text" value="I …

Spring Default Limit for Collection and Array is 256

If you are working in Web Application Development then you may stuck or already had ,then you will get mad of getting this error

How this error emanates in Spring?

i will explain you with example.Suppose you are adding Values in List in JSP using JSTL tag or other.
This list is in Form(HTML).When you POST this form,In behind Stage,Spring does binding of Object and List if present.

But wait...
In Spring,Default limit for array and collection growing is 256.

Why this limit is Set?

seldom you have to send List size greater than this limit.
Spring has set this limit to avoid notable OutOfMemoryErrors in case of large indexes.

So,than What if i want to set Collection greater than this size?

I had told earlier,spring does binding behind.If we tell spring to increase it's size or set collection size behalf of spring.
You you can do it.

We have initBinder,  in Spring  it is called when spring does the binding mechanism
e.g @Mod…