Permalink
Browse files

Avoid “Unspecified Error” in IE

when accessing `document.activeElement` from inside an iframe.
  • Loading branch information...
1 parent 6019b13 commit 69669e4b74c30a6fa2c25751970b17daf53cf88c @jdalton jdalton committed with marijnh Nov 16, 2016
Showing with 11 additions and 9 deletions.
  1. +11 −9 src/util/dom.js
View
@@ -1,4 +1,4 @@
-import { ie, ie_version, ios } from "./browser"
+import { ie, ios } from "./browser"
export function classTest(cls) { return new RegExp("(^|\\s)" + cls + "(?:$|\\s)\\s*") }
@@ -58,18 +58,20 @@ export function contains(parent, child) {
} while (child = child.parentNode)
}
-export let activeElt = function() {
- let activeElement = document.activeElement
+export function activeElt() {
+ // IE and Edge may throw an "Unspecified Error" when accessing document.activeElement.
+ // IE < 10 will throw when accessed while the page is loading or in an iframe.
+ // IE > 9 and Edge will throw when accessed in an iframe if document.body is unavailable.
+ let activeElement
+ try {
+ activeElement = document.activeElement
+ } catch(e) {
+ activeElement = document.body || null
+ }
while (activeElement && activeElement.root && activeElement.root.activeElement)
activeElement = activeElement.root.activeElement
return activeElement
}
-// Older versions of IE throws unspecified error when touching
-// document.activeElement in some cases (during loading, in iframe)
-if (ie && ie_version < 11) activeElt = function() {
- try { return document.activeElement }
- catch(e) { return document.body }
-}
export function addClass(node, cls) {
let current = node.className

0 comments on commit 69669e4

Please sign in to comment.