Determine if jquery has been loaded, if not dynamically loaded

Source: Internet
Author: User
Tags getscript

Method One:

//Only do anything if jQuery isn ' t definedif(typeofJQuery = = ' undefined ') {    if(typeof$ = = ' function ') {        //warning, global varThispageusingotherjslibrary =true; }        functiongetScript (URL, success) {varScript = document.createelement (' script '); SCRIPT.SRC=URL; varHead = document.getElementsByTagName (' head ') [0], done=false; //Attach handlers for all browsersScript.onload = Script.onreadystatechange =function() {                    if(!done && (!) This. readyState | | This. readyState = = ' Loaded ' | | This. readyState = = ' complete ') ) { done=true; //callback function provided as Paramsuccess (); Script.onload= Script.onreadystatechange =NULL;                            Head.removechild (script);                };                };        Head.appendchild (script);        }; GetScript (' Http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js ',function() {            if(typeofjquery== ' undefined ') {                    //Super Failsafe-still somehow failed ...                } Else {                    //JQuery loaded! Make sure-noconflict just in caseFancycode (); if(thispageusingotherjslibrary) {//Run your jQuery Code            } Else {                //Use . Noconflict () and then run your jQuery Code            }                }        }); } Else{//JQuery was already loaded        //Run your jQuery Code};//The code snippet is from: http://www.sharejs.com/codes/javascript/4281

Method Two: Document.Write

varjqueryscriptoutputted =false;functionInitjquery () {//if the JQuery object isn ' t available    if(typeof(jQuery) = = ' undefined ') {            if(!jqueryscriptoutputted) {            //Only output the script once .jqueryscriptoutputted =true; //output the script (load it from Google API)document.write ("<SCR" + "IPT type=" Text/javascript "src=" http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/ Jquery.min.js "></SCR" + "ipt>"); } setTimeout ("Initjquery ()", 50); } Else {                                $(function() {              //Do anything this needs to being done on Document.ready            //don ' t really need this dom ready thing if used in footer        }); }}initjquery ();//The code snippet is from: http://www.sharejs.com/codes/javascript/4281

Determine if jquery has been loaded, if not dynamically loaded

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.