OxJS loader: remove useless Ox.DEBUG, fix IE js onload, use the same code as in Constants.js to get path

This commit is contained in:
rolux 2013-12-04 17:24:48 +01:00
parent d660edbe2b
commit 6cb7269ce1

View file

@ -23,7 +23,6 @@
loadScriptsSerial(data.files, function() {
Ox.LOCALES = data.locales;
Ox.VERSION = data.version;
Ox.DEBUG = true;
Ox.forEach(previousOx, function(value, key) {
if (Ox.isUndefined(Ox[key])) {
Ox[key] = value;
@ -35,13 +34,14 @@
}
function getPath() {
var i, path, scripts = document.getElementsByTagName('script');
for (i = 0; i < scripts.length; i++) {
if (/Ox\.js(\?\d+|)$/.test(scripts[i].src)) {
path = scripts[i].src.replace(/Ox\.js(\?\d+|)$/, '');
var index, regexp = /Ox\.js(\?\d+|)$/,
scripts = document.getElementsByTagName('script'), src;
for (index = scripts.length - 1; index >= 0; index--) {
src = scripts[index].src;
if (regexp.test(src)) {
return src.replace(regexp, '');
}
}
return path;
}
function loadJSON(callback) {
@ -57,6 +57,34 @@
request.send();
}
function loadScript(script, callback) {
var element = document.createElement('script'),
head = document.head
|| document.getElementsByTagName('head')[0]
|| document.documentElement;
element.onload = element.onreadystatechange = function() {
if (
!this.readyState
|| this.readyState == 'loaded'
|| this.readyState == 'complete'
) {
callback();
}
}
element.src = path + script + '?' + time;
element.type = 'text/javascript';
head.appendChild(element);
}
function loadScriptsParallel(scripts, callback) {
var i = 0, n = scripts.length;
while (scripts.length) {
loadScript(scripts.shift(), function() {
++i == n && callback();
});
}
}
function loadScriptsSerial(scripts, callback) {
loadScriptsParallel(scripts.shift(), function() {
if (scripts.length) {
@ -67,31 +95,6 @@
});
}
function loadScriptsParallel(scripts, callback) {
var counter = 0, length = scripts.length;
while (scripts.length) {
loadScript(scripts.shift(), function() {
++counter == length && callback();
});
}
}
function loadScript(script, callback) {
var element = document.createElement('script'),
head = document.head
|| document.getElementsByTagName('head')[0]
|| document.documentElement;
if (/MSIE/.test(navigator.userAgent)) {
// FIXME: find a way to check if css/js have loaded in MSIE
setTimeout(callback, 2500);
} else {
element.onload = callback;
}
element.src = path + script + '?' + time;
element.type = 'text/javascript';
head.appendChild(element);
}
}
};