I have a functionality where depending on the situation I must declare the JQuery library and some other scripts, such as the script Alert.js that a class of the same name. I have tested this functionality in Google Chrome and Firefox, in the first the scripts are always loaded correctly before the execution of the function / class Alert (), but in Firefox the scripts are loaded in an unstable manner, that is, when the Alert function is executed , often jQuery is not yet available, so it is necessary to reload the page so that the second time it works correctly.
To clarify further, the error returned by Firefox in the console are as follows:
First:
ReferenceError: jQuery is not defined
Second:
TypeError: $(...).dialog is not a function
$("#dialog-message").dialog(arrayPropriedades);
The second error code is inside the Alert class, where $ .dialog () is a native JQuery UI function that was not loaded due to the first error reporting that jQuery had not yet been loaded. >
See below the code for this feature:
if (typeof (Alert) === 'undefined') {
var scripts = [
DIR_JS_LIB + 'lib/jquery/jquery.min.js',
DIR_JS_LIB + 'lib/jquery/jquery-ui/jquery-ui.min.js',
DIR_JS_LIB + 'scripts/componentes/dialog/AcaoJanela.js',
DIR_JS_LIB + 'scripts/componentes/dialog/Botao.js',
DIR_JS_LIB + 'scripts/componentes/dialog/Dialog.js',
DIR_JS_LIB + 'scripts/componentes/dialog/Alert.js'
];
for (var i = 0; i < scripts.length; i++) {
include(scripts[i]);
}
}
window.onload = function () {
Alert(window.mensagem, window.titulo, window.botoes);
}
function include(file_path) {
var j = document.createElement("script");
j.src = file_path;
document.getElementsByTagName('head')[0].appendChild(j);
}
PHP code that runs the above script (whose name is defined by the $this->path_load_script
variable):
$str .= '<link rel="stylesheet" type="text/css" href="/app/assets/css/styles.css"/>'
. '<link rel="stylesheet" type="text/css" href="' . DIR_JS_LIB . 'lib/jquery/jquery-ui/jquery-ui.custom.min.css" />'
. "<script>"
. "DIR_JS_LIB = '" . DIR_JS_LIB . "';\n"
. "isColetor = " . (isColetor() ? 'true' : 'false') . ";\n"
. "mensagem = '" . $this->getMensagem() . "';\n"
. "titulo = '" . $this->getTitulo() . "';\n"
. "botoes = {" . implode(',', $this->getBotoes()) . "};\n"
. "try{\n"
. get_called_class() . "(mensagem, titulo, botoes);\n"
. "} catch(err) {\n"
. "var script = document.createElement('script');"
. "script.src='" . $this->path_load_script . "';\n"
. " document.getElementsByTagName('head')[0].appendChild(script);\n"
. "}\n"
. "</script>\n";