1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 * A simple way to be notified (once) when a window becomes
10 * interactive (DOMContentLoaded).
12 * It is based on the chromeEventHandler. This is useful when
13 * chrome iframes are loaded in content docshells (in Firefox
16 * @param nsIDOMWindow win
17 * The content window, owning the document to traverse.
18 * @param Function callback
19 * The method to call when the frame is loaded.
20 * @param String targetURL
21 * (optional) Check that the frame URL corresponds to the provided URL
22 * before calling the callback.
24 onceDOMReady(win, callback, targetURL) {
26 throw new Error("window can't be null or undefined");
28 const docShell = win.docShell;
29 const onReady = function (event) {
30 if (event.target == win.document) {
31 docShell.chromeEventHandler.removeEventListener(
35 // If in `callback` the URL of the window is changed and a listener to DOMContentLoaded
36 // is attached, the event we just received will be also be caught by the new listener.
37 // We want to avoid that so we execute the callback in the next queue.
38 Services.tm.dispatchToMainThread(callback);
42 (win.document.readyState == "complete" ||
43 win.document.readyState == "interactive") &&
44 win.location.href == targetURL
46 Services.tm.dispatchToMainThread(callback);
48 docShell.chromeEventHandler.addEventListener("DOMContentLoaded", onReady);