I'm creating a javascript code using jQuery. But, first, I would like to :
- Test if jQuery already exist in the page
- Load if not
- Load my own jQuery plugin
I've write this small code but I have an error in the start()
function :
jQuery is not defined
My code will be in a js file :
window.addEventListener('DOMContentLoaded', function(){
if (!window.jQuery) {
loadjQuery(onScriptLoad);
}else{
start();
}
});
function onScriptLoad() {
console.log('jQuery is load !');
start();
}
function loadjQuery(callback)
{
//Get jQuery
var script = document.createElement('script');
script.type = "text/javascript";
script.src = "https://code.jquery.com/jquery-1.12.4.min.js";
script.integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=";
script.crossorigin = "anonymous";
document.getElementsByTagName('head')[0].appendChild(script);
callback();
}
function start(){
(function( $ ) {
// Plugin definition.
$.fn.transliveasy = function( options ) {
debug( this );
// ...
};
// Private function for debugging.
function debug( obj ) {
if ( window.console && window.console.log ) {
window.console.log( "Debug : ", obj );
}
}
})( jQuery );
console.log('I am load !');
}