목차
- window.jQuery 예제 - 제이쿼리 라이브러리 로드 여부 체크
- window.jQuery 정의
window.jQuery 예제 - 제이쿼리 라이브러리 로드 여부 체크
<?php
if (!window.jQuery) { // jQuery 로드 안 된 경우 동적으로 추가
var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-3.6.0.min.js';
document.head.appendChild(script);
script.onload = function() {
console.log('jQuery가 동적으로 로드 됨');
// jQuery 로드 후 추가 작업 수행
};
} else {
console.log('jQuery가 이미 로드 됨.');
}
window.jQuery 정의
window는 브라우저의 전역 객체를 가리킴. window.jQuery는 jQuery 라이브러리가 브라우저에서 로드되었는지 확인하는 코드임. 즉, window.jQuery가 존재한다면, jQuery가 정상적으로 로드되어 사용할 수 있다는 의미임.
PS.
- window.jQuery가 undefined일 경우 jQuery가 로드되지 않은 상태이고, 그렇지 않으면 jQuery가 로드된 상태임을 나타냄.
- 이를 통해 jQuery가 없는 환경에서도 적절히 대처 가능.