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

后端 未结 3 570
再見小時候
再見小時候 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);
    }
    
    0 讨论(0)
  • 2021-02-08 19:54

    Given your constraints, I see only two options:

    1. Use window.load event:

      (function() {
          if (window.addEventListener) {
              // Standard
              window.addEventListener('load', jQueryCheck, false);
          }
          else if (window.attachEvent) {
              // Microsoft
              window.attachEvent('onload', jQueryCheck);
          }
          function jQueryCheck() {
              if (typeof jQuery === "undefined") {
                  // No one's loaded it; either load it or do without
              }
          }
      })();
      

      window.load happens very late in the loading cycle, though, after all images are and such loaded.

    2. Use a timeout. The good news is that the timeout can probably be quite short.

      (function() {
          var counter = 0;
      
          doDetect();
          function doDetect() {
              if (typeof jQuery !== "undefined") {
                  // ...jQuery has been loaded
              }
              else if (++counter < 5) { // 5 or whatever
                  setTimeout(doDetect, 10);
              }
              else {
                  // Time out (and either load it or don't)
              }
          }
      })();
      

      You'll have to tune to decide the best values for the counter and the interval. But if jQuery isn't loaded even on the first or second loop, my guess (untested) is that it isn't going to be loaded...unless someone else is doing what you're doing. :-)

    0 讨论(0)
  • 2021-02-08 19:55

    You can use window.onload. This fires after domReady, so jQuery would surely be loaded by this point.

    And check for jQuery, not $. Sometimes people use jQuery with other libraries and use $ for something different.

    However, IMHO, I don't think it's a big deal if jQuery gets loaded twice.

    0 讨论(0)
提交回复
热议问题