Posted about 2 years ago. Visible to the public. Deprecated.

Minimal JavaScript function to detect version of Internet Explorer or Edge

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

If possible your code should detect features, not browsers. 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) }) )

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Henning Koch
Last edit:
5 months ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more