IT

Javascript를 사용하여 jquery 가로 드되었는지 확인

lottoking 2020. 6. 12. 08:28
반응형

Javascript를 사용하여 jquery 가로 드되었는지 확인


내 Jquery 라이브러리가 HTML 페이지에로드되어 있는지 확인하려고합니다. 작동하는지 확인하고 있지만 뭔가 잘못되었습니다. 여기 내가 가진 것입니다 :

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <script type="text/javascript" src="/query-1.6.3.min.js"></script>
        <script type="text/javascript">
          $(document).ready(function(){
             if (jQuery) {  
               // jQuery is loaded  
               alert("Yeah!");
             } else {
               // jQuery is not loaded
               alert("Doesn't Work");
             }
          });
        </script>

뭔가 옳지 않다

글쎄, jQuery를 사용하여 jQuery가 있는지 확인합니다. jQuery 가로 드 $()되지 않으면 다른 라이브러리를 사용하지 않고 해당 라이브러리가 동일한 $()구문 을 공유하지 않는 한 전혀 실행되지 않으며 콜백이 실행되지 않습니다 .

제거하십시오 $(document).ready()( window.onload대신 비슷한 것을 사용 하십시오 ).

window.onload = function() {
    if (window.jQuery) {  
        // jQuery is loaded  
        alert("Yeah!");
    } else {
        // jQuery is not loaded
        alert("Doesn't Work");
    }
}

링크 :

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


링크에 대한 의견에는 몇 가지가 더 있습니다.

if (typeof jQuery == 'function') {...}

//or

if (typeof $== 'function') {...}

// or

if (jQuery) {
    alert("jquery is loaded");
} else {
    alert("Not loaded");
}


이것이이 일을 끝내는 가장 좋은 방법을 다루기를 바랍니다.


if ('undefined' == typeof window.jQuery) {
    // jQuery not present
} else {
    // jQuery present
}

웹 페이지를 검사 할 때 콘솔 탭에서이를 빠르게 수행 할 수 있습니다.

예 :

$ === jQuery

If it returns true it means it's loaded.


Just a small modification that might actually solve the problem:

window.onload = function() {
   if (window.jQuery) {  
       // jQuery is loaded  
       alert("Yeah!");
   } else {
    location.reload();
   }
}

Instead of $(document).Ready(function() use window.onload = function().


You can just type window.jQuery in Console . If it return a function(e,n) ... Then it is confirmed that the jquery is loaded and working successfully.


A quick way is to run a jQuery command in the developer console. On any browser hit F12 and try to access any of the element .

 $("#sideTab2").css("background-color", "yellow");

enter image description here

참고URL : https://stackoverflow.com/questions/7341865/checking-if-jquery-is-loaded-using-javascript

반응형