Skip to content

Commit 69669e4

Browse files
jdaltonmarijnh
authored andcommitted
Avoid “Unspecified Error” in IE
when accessing `document.activeElement` from inside an iframe.
1 parent 6019b13 commit 69669e4

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/util/dom.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ie, ie_version, ios } from "./browser"
1+
import { ie, ios } from "./browser"
22

33
export function classTest(cls) { return new RegExp("(^|\\s)" + cls + "(?:$|\\s)\\s*") }
44

@@ -58,18 +58,20 @@ export function contains(parent, child) {
5858
} while (child = child.parentNode)
5959
}
6060

61-
export let activeElt = function() {
62-
let activeElement = document.activeElement
61+
export function activeElt() {
62+
// IE and Edge may throw an "Unspecified Error" when accessing document.activeElement.
63+
// IE < 10 will throw when accessed while the page is loading or in an iframe.
64+
// IE > 9 and Edge will throw when accessed in an iframe if document.body is unavailable.
65+
let activeElement
66+
try {
67+
activeElement = document.activeElement
68+
} catch(e) {
69+
activeElement = document.body || null
70+
}
6371
while (activeElement && activeElement.root && activeElement.root.activeElement)
6472
activeElement = activeElement.root.activeElement
6573
return activeElement
6674
}
67-
// Older versions of IE throws unspecified error when touching
68-
// document.activeElement in some cases (during loading, in iframe)
69-
if (ie && ie_version < 11) activeElt = function() {
70-
try { return document.activeElement }
71-
catch(e) { return document.body }
72-
}
7375

7476
export function addClass(node, cls) {
7577
let current = node.className

0 commit comments

Comments
 (0)