Read more

Minimal JavaScript function to detect version of Internet Explorer or Edge

Henning Koch
June 23, 2018Software engineer at makandra GmbH

This function hasn't been tested with the new Chromium Edge.

If possible your code should detect features, not browsers Show archive.org snapshot . But sometimes you just need to sniff the browser. And when you do, you're probably fighting a Microsoft product.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

The following function returns a Number like 10, 11, 12, 13 for Internet Explorer or Edge (anything above 11 is Edge). It returns undefined for any other browser.

function ieVersion(uaString) {
  uaString = uaString || navigator.userAgent;
  var match = /\b(MSIE |Trident.*?rv:|Edge\/)(\d+)/.exec(uaString);
  if (match) return parseInt(match[2])
}

Here are the tests:

describe('ieVersion()', () => {

  it('returns 10 for Internet Explorer 10', () => {
    version = up.browser.ieVersion('Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)')
    expect(version).toEqual(10)
  })

  it('returns 11 for Internet Explorer 11', () => {
    version = up.browser.ieVersion('Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko')
    expect(version).toEqual(11)
  })

  it('returns 12 for Edge 12', () => {
    version = up.browser.ieVersion('Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0')
    expect(version).toEqual(12)
  })

  it('returns 13 for Edge 13', () => {
    version = up.browser.ieVersion('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586')
    expect(version).toEqual(13)
  })
)
Posted by Henning Koch to makandra dev (2018-06-23 23:09)