How to determine if jQuery is running on a page (even if jQuery is /after/ the detection script)

后端 未结 3 572
再見小時候
再見小時候 2021-02-08 19:04

I\'m working on a do-dad that can be embedded in a page like a youtube video. The particular effect I want needs jQuery to work.

I want to load jQuery on the condition t

3条回答
  •  难免孤独
    2021-02-08 19:40

    I've been using this code for to do this very thing for a while now. It also checks for a minimum version of jQuery (in our case, we're still using 1.4.2) before loading:

    /* Checks if JQuery is loaded... if not, load it. */
    /* Remember to update minimum version number when updating the main jquery.min.js file. */
    
    if (typeof jQuery != 'undefined') {
        /* jQuery is already loaded... verify minimum version number of 1.4.2 and reload newer if needed */
        if (/1\.(0|1|2|3|4)\.(0|1)/.test(jQuery.fn.jquery) || /^1.1/.test(jQuery.fn.jquery) || /^1.2/.test(jQuery.fn.jquery)|| /^1.3/.test(jQuery.fn.jquery)) {
            loadJQ();
        }
    } else {
        loadJQ();
    }
    
    /* loads jQuery if not already loaded, or if not a recent enough version */
    function loadJQ() {
        /* adds a link to jQuery to the head, instead of inline, so it validates */ 
        var headElement = document.getElementsByTagName("head")[0];
        linkElement=document.createElement("script");
        linkElement.src="../scripts/lib/jquery.min.js";
        linkElement.type="text/javascript";
        headElement.appendChild(linkElement);
    }
    

提交回复
热议问题