Minimal JavaScript function to detect version of Internet Explorer or Edge

Updated . Posted . Visible to the public. Deprecated.

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.

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)
  })
)
Henning Koch
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2018-06-23 21:09)