Answer the question
In order to leave comments, you need to log in
How are scripts loaded?
There is such a page
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="container"></div>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>
containerWrap = document.getElementById("container");
var jQueryMinJs = document.createElement("script");
jQueryMinJs.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
containerWrap.appendChild(jQueryMinJs);
var script__ = document.createElement("script");
jQueryUiMinJs.src = "js/script__.js";
containerWrap.appendChild(script__);
$ is not definedas if jQuery didn't load on the page. How to solve this problem?
Answer the question
In order to leave comments, you need to log in
These scripts are loaded asynchronously.
Try something like this:
containerWrap = document.getElementById("container");
var jQueryMinJs = document.createElement("script");
jQueryMinJs.onload = loadOtherScripts;
jQueryMinJs.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
containerWrap.appendChild(jQueryMinJs);
function loadOtherScripts() {
$.getScript('js/script__.js', function () {
console.log('script__.js loaded');
});
}
if the scripts are declared in the document - they are loaded synchronously, since the sequence is important due to the dependence of one script on another, if they are added dynamically to the body of the document - asynchronously.
if you can’t do without a synchronous declaration, you can use onload callback, as described above.
And if you add jQueryMinJs.async = false;
the "Looking Ahead" Block at the end of the lesson - https://learn.javascript.ru/external-script
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question