2013-07-12 4 views
2

안녕하세요 저는 JavaScript 및 PHP로 타사 위젯을 제작해야합니다. 이 위젯은 jQuery와 jQuery UI에서 사용할 필요가 있으며 앞으로는 다른 jQuery 라이브러리와 플러그인에서 사용해야 할 것입니다. 내 클라이언트가이 위젯을 자신의 사이트에 넣었을 때 jQuery와 jQuery UI가 이미로드되어 있는지, 그리고로드되어 있지 않은지 알 필요가 있습니다. 나는 무언가를 만들었지 만 작동하지 않습니다.jQuery 및 jQuery UI가로드되었는지 확인하십시오.

<script> 
if (typeof jQuery == 'undefined') { 
    // jQuery is not loaded 
    //alert('jQuery is not loaded'); 
    var script = document.createElement('script'); 
    script.type = "text/javascript"; 
    script.src = "http://code.jquery.com/jquery-1.9.1.js"; 
    document.getElementsByTagName('head')[0].appendChild(script); 

    var script_ui = document.createElement('script'); 
    script_ui.type = "text/javascript"; 
    script_ui.src = "http://code.jquery.com/ui/1.10.3/jquery-ui.js"; 
    document.getElementsByTagName('head')[0].appendChild(script_ui); 

} else { 
    // jQuery is loaded 
    //alert('jQuery is loaded'); 
    if (typeof jQuery.ui !== 'undefined') { 
     // ui plugin exists 
     alert('ui plugin exists'); 
    } else { 
     // ui plugin DOES NOT exist 
     //alert('ui plugin DOES NOT exist'); 
     var script_ui = document.createElement('script'); 
     script_ui.type = "text/javascript"; 
     script_ui.src = "http://code.jquery.com/ui/1.10.3/jquery-ui.js"; 
     document.getElementsByTagName('head')[0].appendChild(script_ui); 
    } 
} 
</script> 

잘 작동하지만 jQuery datepicker로 작업하려고하면 ReferenceError가 발생합니다. jQuery가 정의되지 않았습니다.

<script> 
jQuery(document).ready(function() { 
    // Handler for .ready() called. 
    jQuery(function() { 
     jQuery("#datepicker").datepicker(); 
    }); 
}); 
</script> 

JQuery와 (문서) .ready (함수() 그것은 내가 문제를 정확하게 이해하는 경우

+0

가능 [어떻게, JQuery와 및 JQuery와 UI가 설치되어 있는지 감지 할 수의 중복과 어떤 버전을 설치되어 있습니까?] (http://stackoverflow.com/questions/9934424/how-can-i-detect-if-jquery-and-jquery-ui-are-installed-and-what-versions-are-in) –

답변

6

이 트릭을 할해야 ... HELLP하십시오 작동하지 않는조차없이 이미로드 된 경우 jQuery를() 함수는 정의된다.

if (jQuery) { //jQuery is loaded } 

당신은 당신이 필요로하는 기능이 작업을 수행 할 수는 jQuery를, jQuery를-UI 또는 뭔가 다른 여부.

<script> 
jQuery(document).ready(function() { 
    // Handler for .ready() called. 
    jQuery(function() { 
     jQuery("#datepicker").datepicker(); 
    }); 
}); 
</script> 

는, jQuery를 반드시로드가 완료되지 않을 수 있습니다 도달 시간으로

+0

안녕 (jQuery) 여전히 작동하지 않는 경우 riply 내가 테스트 주셔서 감사. ReferenceError : jQuery가 정의되지 않았습니다. – pey22

+0

jQuery 변수에 액세스하는 것은 해당 변수가 정의되지 않은 경우 오류입니다. jQuery가로드되지 않은 경우입니다. 시도해보십시오 :'if (typeof jQuery! = 'undefined') ' – basic6

0

, 브라우저가 다음에해야합니까 알고하지 않습니다, 그것을 실행하려고 나는 전혀 모른다 "을 알려줍니다 무엇을 jQuery는 "입니다. 나중에 같은 코드 블록을 다시 호출하면 정상적으로 작동합니다.

이 문제를 해결하는 가장 간단한 방법은 $ (document) .ready() 이벤트 핸들러를 별도의 .js 스크립트에 넣고 헤더에 해당 스크립트를 포함시키는 것입니다. 그러면 jQuery가 이벤트 핸들러를 호출 할 때마다 다른 방법은 아닙니다. 나는 이것을 보여주기 위해 바이올린을 만든 : http://jsfiddle.net/Cjv2k/4/

+0

안녕하세요 jQuery.getScript (serverFQDN +'/cal.js '); 덕분에 모두 . – pey22

0

할 수 있습니다 루프 jQuery 오브젝트를 사용하여이 유효 때까지

var checkJQ = setInterval(function() { 
    if(jQuery) { 
     console.log('jQuery loaded'); 
     clearInterval(checkJQ); 
    } 
}, 300);