2011-02-18 2 views
4

Google Analytics가 사용되는 것과 유사한 한 줄의 코드에 웹 사이트에 포함될 수있는 자바 스크립트 웹 서비스를 개발하려고합니다. 내 자바 스크립트 코드에서 jQuery를 사용하고 싶습니다만, 이미 호스팅 웹 페이지에있는 모든 js 라이브러리와 충돌하지 않기를 바랍니다. 내 코드 라이브러리 내에서 JQuery와 사용할 수 있도록 ...자바 스크립트 API 내에서 jquery를 사용하는 방법은 무엇입니까?

document.write('<script type="text/javascript" ' + 
      'src="http://mydomain.com/js/jquery-1.4.2.min.js"></script>'); 

:

는 기본적으로, 최종 사용자가 자신의 웹 페이지에 포함 할 것 내 주요 자바 스크립트 파일 안에, 내가 좋아하는 일을 할 것입니다. 나는 이것이 javascript API에서 jquery를 사용하는 적절한 방법인지 또는 더 많은 고려 사항이 있는지 궁금합니다. 누군가가 제안 할 수있는 예제 나 기사는 고맙게 생각합니다.

답변

0

백은 내가 jQuery를 활성화의 bookmarklet generator 뻔뻔한 플러그했다있다.

if (!window.zbooks) 
    { 
    //if zbooks hasn't been set, initialize it 

    //s used for the Script element 
    var s = document.createElement('script'); 
    //r used for the Ready state 
    var r = false; 
    //set the script to the latest version of jQuery 
    s.setAttribute('src', 'http://code.jquery.com/jquery-latest.min.js'); 
    //set the load/readystate events 
    s.onload = s.onreadystatechange = function() 
    { 
/** 
* LOAD/READYSTATE LOGIC 
* execute if the script hasn't been ready yet and: 
* - the ready state isn't set 
* - the ready state is complete 
* - note: readyState == 'loaded' executes before the script gets called so 
*  we skip this event because it wouldn't have loaded the init event yet. 
*/ 
     if (!r && (!this.readyState || this.readyState == 'complete')) 
     { 
     //set the ready flag to true to keep the event from initializing again 
     r = true; 
     //prevent jQuery conflicts by placing jQuery in the zbooks object 
     window.zbooks = {'jQuery':jQuery.noConflict()}; 
     //make a new zbook 
     window.zbooks[n] = new zbooks(c); 
     } 
    }; 
    //append the jQuery script to the body 
    b.appendChild(s); 
    } 
관련 문제