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
  • 9,692
  • 38
  • 168
  • 320
  • 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
  • 46,935
  • 21
  • 128
  • 175
4

convert it to a integer

versionInteger = parseInt(version, 10)
silly
  • 7,565
  • 2
  • 23
  • 36
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
  • 37,856
  • 8
  • 63
  • 74
1
version = version.split('.')[0];
Engineer
  • 45,891
  • 11
  • 86
  • 90