2

I am trying to figure out how to go about detecting what browser is being used. Some report the version as being 10.0, 10, 4.6 or 5. How can i just get the whole number without any .x at the end (if it has one to begin with)?

I currently use this:

version = version.substring(0,3);

Which works if its a whole number but not if it has a period in them.

David

StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • duplicated question : http://stackoverflow.com/a/596503/889678 – mgraph Mar 23 '12 at 14:35
  • Please don't do version detection, if you must serve different scripts for different browser, use [object detection](http://www.quirksmode.org/js/support.html) – Lie Ryan Mar 23 '12 at 14:39

4 Answers4

5

What about something like...

var index = version.indexOf(".");
if(index != -1){
   version = version.substring(0, index);
}

Here is a working example

musefan
  • 47,875
  • 21
  • 135
  • 185
4

convert it to a integer

versionInteger = parseInt(version, 10)
silly
  • 7,789
  • 2
  • 24
  • 37
3

You can use parseInt() with a radix of 10 to convert it to just the whole number part.

parseInt(10.5, 10);

jsFiddle demo

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
1
version = version.split('.')[0];
Engineer
  • 47,849
  • 12
  • 88
  • 91