Can a javascript check for jQuery and load it if not already present?

后端 未结 3 1097
囚心锁ツ
囚心锁ツ 2020-12-21 15:57

I\'m just looking for a simple javascript that can check to see if the jQuery library is already loaded and if not, loads it.

Thanks in advance if you have a solutio

相关标签:
3条回答
  • 2020-12-21 16:06
    if (typeof jQuery == 'undefined') {  
        // jQuery is not loaded => load it:
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js';
        document.getElementsByTagName('body')[0].appendChild(script);
    }
    
    0 讨论(0)
  • 2020-12-21 16:11

    Try this

    if(typeof($) == 'undefined' || typeof(jQuery) == 'undefined'){//JQuery do not exist
        //Load the Jquery from your site or any CDN
    }
    
    0 讨论(0)
  • 2020-12-21 16:25

    Load script from Javascript by adding a <script> tag:

    var fileref=document.createElement('script')
    fileref.setAttribute("type","text/javascript")
    fileref.setAttribute("src", filename)
    document.getElementsByTagName("head")[0].appendChild(fileref)
    
    0 讨论(0)
提交回复
热议问题