diff --git a/lib/ace/lib/dom.js b/lib/ace/lib/dom.js index 81c9773a..4df72736 100644 --- a/lib/ace/lib/dom.js +++ b/lib/ace/lib/dom.js @@ -33,6 +33,12 @@ define(function(require, exports, module) { var XHTML_NS = "http://www.w3.org/1999/xhtml"; +exports.getDocumentHead = function(doc) { + if (!doc) + doc = document; + return doc.head || doc.getElementsByTagName("head")[0] || doc.documentElement; +} + exports.createElement = function(tag, ns) { return document.createElementNS ? document.createElementNS(ns || XHTML_NS, tag) : @@ -134,8 +140,7 @@ exports.importCssString = function importCssString(cssText, id, doc) { if (id) style.id = id; - var head = doc.head || doc.getElementsByTagName("head")[0] || doc.documentElement; - head.appendChild(style); + exports.getDocumentHead(doc).appendChild(style); } }; @@ -147,8 +152,7 @@ exports.importCssStylsheet = function(uri, doc) { link.rel = 'stylesheet'; link.href = uri; - var head = doc.head || doc.getElementsByTagName("head")[0] || doc.documentElement; - head.appendChild(link); + exports.getDocumentHead(doc).appendChild(link); } }; diff --git a/lib/ace/lib/net.js b/lib/ace/lib/net.js index e125582e..3869da45 100644 --- a/lib/ace/lib/net.js +++ b/lib/ace/lib/net.js @@ -7,6 +7,7 @@ */ define(function(require, exports, module) { "use strict"; +var dom = require("./dom"); exports.get = function (url, callback) { var xhr = new XMLHttpRequest(); @@ -22,18 +23,19 @@ exports.get = function (url, callback) { }; exports.loadScript = function(path, callback) { - var head = document.head || document.getElementsByTagName('head')[0]; + var head = dom.getDocumentHead(); var s = document.createElement('script'); s.src = path; head.appendChild(s); - if (s.onload !== undefined) - s.onload = callback; - else - s.onreadystatechange = function () { - this.readyState == 'loaded' && callback(); - }; + s.onload = s.onreadystatechange = function(_, isAbort) { + if (isAbort || !s.readyState || s.readyState == "loaded" || s.readyState == "complete") { + s = s.onload = s.onreadystatechange = null; + if (!isAbort) + callback(); + } + }; }; });