isIE
Detects if the current browser is Internet Explorer. Checks for legacy document.documentMode
(IE 6–11).
1/**
2 * Detects if the current browser is Internet Explorer.
3 *
4 * Checks for legacy `document.documentMode` (IE 6–11).
5 *
6 * @returns True if Internet Explorer is detected.
7 */
8export function isIE(): boolean {
9 if (typeof window === 'undefined') return false;
10 return !!(document as any).documentMode;
11}
Reliable Detection Method
Uses
document.documentMode
, a well-known and dependable property unique to Internet Explorer versions 6 through 11.Safe for Non-Browser Environments
Includes a guard clause to avoid errors in server-side rendering or non-browser runtimes.
Lightweight and Fast
Executes a simple boolean check with negligible performance impact.
Tests | Examples
1const originalDocumentMode = (document as any).documentMode;
2
3afterEach(() => {
4 (document as any).documentMode = originalDocumentMode;
5});
6
7test('returns true if documentMode is present (IE)', () => {
8 (document as any).documentMode = 11;
9 expect(isIE()).toBe(true);
10});
11
12test('returns false if documentMode is undefined (not IE)', () => {
13 delete (document as any).documentMode;
14 expect(isIE()).toBe(false);
15});
Common Use Cases
Displaying Deprecation Notices
Inform users that IE is no longer supported and recommend modern browsers.
Conditional Polyfill Loading
Dynamically load scripts to patch missing features when IE is detected.
Graceful Degradation
Disable or simplify advanced features that won’t work properly in IE.
Analytics and Logging
Track usage of unsupported browsers for business or technical insights.