I am working on JavaScript widgets. Below is my code calling widgets html page.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="http://localhost/widget1/widget1.js" type="text/javascript"></script>
<script src="http://localhost/widget1/widget2.js" type="text/javascript"></script>
</head>
<body>
<div style="width: 100%; height: 100%;display: inline-block;background-color:#eee">
<h1 align="center"><a href="#">Demo test</a></h1>
<div id="container1" style="width: 45%; height: 300px;display: inline-block;margin:30px"></div>
<div id="container2" style="width: 45%; height: 300px;display: inline-block;margin:30px"></div>
<br>
</div>
</body>
</html>
and below is my widgets1.js and widgets2.js files:
(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (typeof jQuery == undefined || typeof jQuery != '1.9.1') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type", "text/javascript");
script_tag.setAttribute("id", "jqueryLib");
script_tag.setAttribute("src", "http://code.jquery.com/jquery-1.9.1.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function() { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
//}
} else {
//The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
})();
On html page firstly I am loading widgets1.js file and secondly I am loading widgets2.js file.I am checking http://code.jquery.com/jquery-1.9.1.js file if not define to load.
This file is loaded in widgets1.js file but http://code.jquery.com/jquery-1.9.1.js file get loaded again in widgets2.js file which i don't won't to load again.
I am checking with typeof jQuery == undefined but not validate and file get loaded two times.