Dynamically load jquery if not present

With this code you can retrieve if jquery is set in your script, if jquery is not set then the script load it dinamically and set it.
(function () { var jQuery; if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') { var script_tag = document.createElement('script'); script_tag.setAttribute("type", "text/javascript"); script_tag.setAttribute("src", pathWidget + "/scripts/jquery-1.7.min.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; } (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag); } else { jQuery = window.jQuery; main(); } function scriptLoadHandler() { jQuery = window.jQuery.noConflict(true); main(); } function main() { jQuery(document).ready(function ($) { }); } })();
tags: javascript jquery function
Related Posts
- Is jQuery going to die in 2019?
- 14 JavaScript Animation Libraries For 2019
- Javascript: Introduction to ES6 classes
- Top free JavaScript User Authentication Libraries
- Best 5 Javascript NewsTicker Plugins
- Which Javascript frameworks will rule in 2019?
- Async/Await how they really work: an example
- ES2019 JavaScript and its new features
- Vue.js: a quick start guide for beginners. Part 2
- 15 Awesome games made with HTML5 and JavaScript
- Developing a JavaScript video game: Part 2
- Vue.js: a quick start guide for beginners. Part 1.