복붙노트

[JQUERY] JQuery와로드 된 경우 false의 경우,로드 확인

JQUERY

JQuery와로드 된 경우 false의 경우,로드 확인

해결법


  1. 1.이 같은 아마 뭔가 :

    이 같은 아마 뭔가 :

    <script>
    if(!window.jQuery)
    {
       var script = document.createElement('script');
       script.type = "text/javascript";
       script.src = "path/to/jQuery";
       document.getElementsByTagName('head')[0].appendChild(script);
    }
    </script>
    

  2. 2.IE 이후 "(! jQuery를)이 경우"오류를 반환합니다 사용하지 마십시오 : JQuery와 '정의'입니다

    IE 이후 "(! jQuery를)이 경우"오류를 반환합니다 사용하지 마십시오 : JQuery와 '정의'입니다

    대신 사용하는 경우 (대해서 typeof jQuery를 == '정의되지 않은')

    <script type="text/javascript">
    if (typeof jQuery == 'undefined') {
        var script = document.createElement('script');
        script.type = "text/javascript";
        script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js";
        document.getElementsByTagName('head')[0].appendChild(script);
    }
    </script>
    

    또한 JQuery와 헤더에 추가 한 후로드되었는지 확인해야합니다. 그렇지 않으면 당신은 페이지 이미지가있는 경우 느린입니다 위해 window.onload 이벤트를 기다려야 할 것이다. 당신이 $ (문서) .ready (기능을 사용할 수있는 편리함을 가지고 있지 때문에 여기 JQuery와 파일이로드되었는지 확인 샘플 스크립트의 ...

    http://neighborhood.org/core/sample/jquery/append-to-head.htm


  3. 3.방법 1 :

    방법 1 :

    if (window.jQuery) {  
        // jQuery is loaded  
    } else {
        // jQuery is not loaded
    }
    

    방법 2 :

    if (typeof jQuery == 'undefined') {  
        // jQuery is not loaded
    } else {
        // jQuery is loaded
    }
    

    jquery.js 파일이로드되지 않을 경우, 우리는 그렇게 좋아하는 부하를 강제 할 수 있습니다 :

    if (!window.jQuery) {
      var jq = document.createElement('script'); jq.type = 'text/javascript';
      // Path to jquery.js file, eg. Google hosted version
      jq.src = '/path-to-your/jquery.min.js';
      document.getElementsByTagName('head')[0].appendChild(jq);
    }
    

  4. 4.이 시도 :

    이 시도 :

    <script>
      window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
    </script>
    

    하지가 지정된 경로에서 동적으로 하나를 추가 할 경우 확인 jQuery를가 사용할 수 없거나없는 경우.

    참고 : 시뮬레이션에 "include_once 문"jQuery를위한

    또는

    JS 상당 include_once 문. 참조 : https://raw.github.com/kvz/phpjs/master/functions/language/include_once.js

    function include_once (filename) {
      // http://kevin.vanzonneveld.net
      // +   original by: Legaev Andrey
      // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
      // +   improved by: Michael White (http://getsprink.com)
      // +      input by: Brett Zamir (http://brett-zamir.me)
      // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
      // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
      // -    depends on: include
      // %        note 1: Uses global: php_js to keep track of included files (though private static variable in namespaced version)
      // *     example 1: include_once('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js');
      // *     returns 1: true
      var cur_file = {};
      cur_file[this.window.location.href] = 1;
    
      // BEGIN STATIC
      try { // We can't try to access on window, since it might not exist in some environments, and if we use "this.window"
        //    we risk adding another copy if different window objects are associated with the namespaced object
        php_js_shared; // Will be private static variable in namespaced version or global in non-namespaced
        //   version since we wish to share this across all instances
      } catch (e) {
        php_js_shared = {};
      }
      // END STATIC
      if (!php_js_shared.includes) {
        php_js_shared.includes = cur_file;
      }
      if (!php_js_shared.includes[filename]) {
        if (this.include(filename)) {
          return true;
        }
      } else {
        return true;
      }
      return false;
    }
    

  5. 5.당신이 추가 머리가있을 수 있지만 그것은 모든 브라우저에서 작동하지 않을 수 있습니다. 이것은 내가 지속적으로 작동 할 수있는 유일한 방법이었다.

    당신이 추가 머리가있을 수 있지만 그것은 모든 브라우저에서 작동하지 않을 수 있습니다. 이것은 내가 지속적으로 작동 할 수있는 유일한 방법이었다.

    <script type="text/javascript">
    if (typeof jQuery == 'undefined') {
      document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"><\/script>');        
      } 
    </script>
    

  6. 6.jQuery를 같은 여러 가지 방법에 의해로드되지 않거나 확인할 수 있습니다 :

    jQuery를 같은 여러 가지 방법에 의해로드되지 않거나 확인할 수 있습니다 :

    if (typeof jQuery == 'undefined') {
    
        // jQuery IS NOT loaded, do stuff here.
    
    }
    

    if (typeof jQuery == 'function')
    //or
    if (typeof $== 'function')
    

    if (jQuery) {
        // This will throw an error in STRICT MODE if jQuery is not loaded, so don't use if using strict mode
        alert("jquery is loaded");
    } else {
        alert("Not loaded");
    }
    

    if( 'jQuery' in window ) {
        // Do Stuff
    }
    

    이제 jQuery를로드하지 않은 경우 확인 후, 당신은 다음과 같이 jQuery를로드 할 수 있습니다 :

        // This part should be inside your IF condition when you do not find jQuery loaded
        var script = document.createElement('script');
        script.type = "text/javascript";
        script.src = "http://code.jquery.com/jquery-3.3.1.min.js";
        document.getElementsByTagName('head')[0].appendChild(script);
    

  7. 7.이전 게시물 그러나 나는 여러 장소에서 테스트 무슨 좋은 해결책을했다.

    이전 게시물 그러나 나는 여러 장소에서 테스트 무슨 좋은 해결책을했다.

    https://github.com/CreativForm/Load-jQuery-if-it-is-not-already-loaded

    암호:

    (function(url, position, callback){
        // default values
        url = url || 'https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js';
        position = position || 0;
    
        // Check is jQuery exists
        if (!window.jQuery) {
            // Initialize <head>
            var head = document.getElementsByTagName('head')[0];
            // Create <script> element
            var script = document.createElement("script");
            // Append URL
            script.src = url;
            // Append type
            script.type = 'text/javascript';
            // Append script to <head>
            head.appendChild(script);
            // Move script on proper position
            head.insertBefore(script,head.childNodes[position]);
    
            script.onload = function(){
                if(typeof callback == 'function') {
                    callback(jQuery);
                }
            };
        } else {
            if(typeof callback == 'function') {
                callback(jQuery);
            }
        }
    }('https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js', 5, function($){ 
        console.log($);
    }));
    

    GitHub의에서 잘 설명하지만 generaly이 기능을 사용하면 HTML 코드에서 아무 곳이나 추가 할 수 있으며 이미로드되지 않은 경우 jQuery를 초기화합니다.


  8. 8.

    var f = ()=>{
        if (!window.jQuery) {
            var e = document.createElement('script');
            e.src = "https://code.jquery.com/jquery-3.2.1.min.js";
            e.onload = function () {
                jQuery.noConflict();
                console.log('jQuery ' + jQuery.fn.jquery + ' injected.');
            };
            document.head.appendChild(e);
        } else {
            console.log('jQuery ' + jQuery.fn.jquery + '');
        }
    };
    f();
    

  9. 9.

    <script>
    if (typeof(jQuery) == 'undefined'){
            document.write('<scr' + 'ipt type="text/javascript" src=" https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></scr' + 'ipt>');
    }
    </script>
    

  10. 10.나는 내 프로젝트에 대한 CDN을 사용하고 있습니다 및 대체 처리의 일환으로, 내가 코드를 아래에 사용 된

    나는 내 프로젝트에 대한 CDN을 사용하고 있습니다 및 대체 처리의 일환으로, 내가 코드를 아래에 사용 된

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script type="text/javascript">
                    if ((typeof jQuery == 'undefined')) {
                        document.write(unescape("%3Cscript src='/Responsive/Scripts/jquery-1.9.1.min.js' type='text/javascript'%3E%3C/script%3E"));   
                    }
    </script>
    

    다만, 확인 난 CDN 참조를 제거하고 코드를 실행한다. 그 깨진하고 대해서 typeof jQuery를 같은 루프 함수로 오는 대신 정의되지 않은 경우에 입력 한 적이 없어요.

    이 때문에 기능을 반환하고 난 JQuery와 1.9.1를 사용하고 있기 때문에 내 암호를 해독 JQuery와 1.6.1의 캐시 이전 버전입니다. 내가 JQuery와의 정확한 버전을 필요로하는, 난, 다음과 같이 코드를 수정

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
                if ((typeof jQuery == 'undefined') || (jQuery.fn.jquery != "1.9.1")) {
                    document.write(unescape("%3Cscript src='/Responsive/Scripts/jquery-1.9.1.min.js' type='text/javascript'%3E%3C/script%3E"));   
                }
    </script>
    
  11. from https://stackoverflow.com/questions/1828237/check-if-jquery-has-been-loaded-then-load-it-if-false by cc-by-sa and MIT license