From 4504b78613b4a68b838dd887c8b23ec5a71fe779 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Mon, 12 Jan 2015 11:55:20 +0000 Subject: Fix race condition on the options page (better). Uses document.readyState for all onDOMContentLoaded dependent functions. This should fix the same race condition as #1426 for all cases. --- lib/dom_utils.coffee | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'lib/dom_utils.coffee') diff --git a/lib/dom_utils.coffee b/lib/dom_utils.coffee index 762c6418..7a75dd6a 100644 --- a/lib/dom_utils.coffee +++ b/lib/dom_utils.coffee @@ -2,10 +2,11 @@ DomUtils = # # Runs :callback if the DOM has loaded, otherwise runs it on load # - documentReady: do -> - loaded = false - window.addEventListener("DOMContentLoaded", -> loaded = true) - (callback) -> if loaded then callback() else window.addEventListener("DOMContentLoaded", callback) + documentReady: (func) -> + if document.readyState == "loading" + window.addEventListener "DOMContentLoaded", func + else + func() # # Adds a list of elements to a page. @@ -178,12 +179,5 @@ DomUtils = event.preventDefault() @suppressPropagation(event) - # Calls func either now (if the DOM has already loaded), or when the DOM is loaded. - runWhenDOMLoaded: (func) -> - if document.readyState == "loading" - window.addEventListener "DOMContentLoaded", func - else - func() - root = exports ? window root.DomUtils = DomUtils -- cgit v1.2.3