Cita:
Iniciado por Panino5001 Podrías verificar si los elementos han cargado antes de llamarlos, con algo como esto:
Muchas gracias, eso era lo que necesitaba, los eventos. Al final lo he dejado así, por si a alguien le interesa:
Código PHP:
dynamicLoader = {
callback: null,
/**
* Dynamically loads the component indicated (if it wasn't loaded before).
* The component does not need to tell the loader that it's ready.
* @param component The component's name.
* @param callback The function to be called when loading is finished.
*/
load: function(component, callback){
this.callback = callback;
// Check whether the component was already loaded
var already = false;
var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length && !already; i++) {
if (scripts[i].src == SITEURL + '/rpc.php?mod=DynamicLoad&act=' + component)
already = true;
}
if (!already) {
// component not loaded yet
var script = document.createElement("script");
script.type = "text/javascript";
script.src = SITEURL + '/rpc.php?mod=DynamicLoad&act=' + component;
// add event to call the callback when the script is loaded
if (script.addEventListener) {
// for firefox-like browsers
script.addEventListener('load', function(){
console.log('Script loaded: addEventListener');
dynamicLoader.done();
}, false);
}else {
// for IE browsers
script.onreadystatechange = function(){
console.log('Script loaded: onreadystatechange=', script.readyState);
if (script.readyState == 'complete' || script.readyState == 'loaded')
dynamicLoader.done();
}
}
document.getElementsByTagName("head")[0].appendChild(script);
}
else {
// component already loaded. Just call the callback
dynamicLoader.done();
}
},
/**
* Tell the dynamic loader to call the callback function after a module is loaded.
*/
done: function(){
if (this.callback)
this.callback();
this.callback = null;
}
};
Se llama a load con el nombre del componente y la función a llamar cuando termine. El componente se carga desde una url específica en mi caso, cambiarlo con lo que uséis.