사용자 브라우저에서 jQuery 버전 확인

Habdul Hazeez 2023년6월21일
  1. typeof 연산자를 사용하여 jQuery 버전 가져오기
  2. window 개체를 사용하여 jQuery 버전 가져오기
사용자 브라우저에서 jQuery 버전 확인

이 기사에서는 웹 페이지에서 jQuery 버전을 가져오는 두 가지 방법을 설명합니다. 첫 번째 방법은 웹 페이지에 jQuery가 로드된 경우 버전을 확인하고 두 번째 방법은 window 개체에 jQuery가 있는 경우 버전을 확인합니다.

typeof 연산자를 사용하여 jQuery 버전 가져오기

웹 페이지가 jQuery 라이브러리를 로드하면 jQuery 키워드 또는 달러( $) 기호를 통해 사용할 수 있게 됩니다. 이를 통해 typeof 연산자를 사용하여 $ 또는 jQuery가 존재하는지 확인할 수 있습니다.

확인 결과 true가 반환되면 웹 브라우저에 jQuery가 존재하므로 버전 번호를 인쇄할 수 있습니다.

이것이 우리가 다음 코드에서 수행한 작업입니다. 다음은 웹 브라우저의 출력입니다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>01-get-jQuery-version</title>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js" integrity="sha512-aVKKRRi/Q/YV+4mjoKBsE4x3H+BkegoM/em46NNlCqNTmUYADjBbeNefNxYV7giUp0VxICtqdrbqU7iVaeZNXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
    <script>
        // Check if jQuery exists using the typeof
        // operator. The code in the if block will
        // run ONLY if jQuery exists in the user's
        // web browser
        if (typeof jQuery !== undefined) {
            // Show the jQuery version number in the
            // alert dialog box.
            alert("The current jQuery version is: " + jQuery.fn.jquery);
        }
    </script>
</body>
</html>

출력:

typeof Operator - Firefox 104.0.2의 jQuery 버전

window 개체를 사용하여 jQuery 버전 가져오기

웹 브라우저가 jQuery 라이브러리를 로드한 경우 window 개체에서 해당 별칭을 사용할 수 있습니다. 이 지식으로 window 개체에서 jQuery를 확인하고 버전 번호를 얻을 수 있습니다.

이전 예제와 달리 jQuery().jquery를 사용하여 버전을 가져옵니다. 이렇게 하면 개체가 생성되므로 원하지 않는 경우 첫 번째 예를 사용하십시오.

다음에서는 버전을 인쇄하기 전에 window 개체에 jQuery가 포함되어 있는지 확인합니다.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>02-get-jQuery-version</title>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js" integrity="sha512-aVKKRRi/Q/YV+4mjoKBsE4x3H+BkegoM/em46NNlCqNTmUYADjBbeNefNxYV7giUp0VxICtqdrbqU7iVaeZNXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
     <style>
         body {
            background-color: #1a1a1a;
         }
     </style>
</head>
<body>
    <script>
        // Check if jQuery exists in the window
        // object.
        if (window.jQuery) {
            // Show the jQuery version number in the
            // alert dialog box.
            alert("The current jQuery version is: " + jQuery().jquery);
        }
    </script>
</body>
</html>

출력:

window 객체 - Firefox 104.0.2의 jQuery 버전

Habdul Hazeez avatar Habdul Hazeez avatar

Habdul Hazeez is a technical writer with amazing research skills. He can connect the dots, and make sense of data that are scattered across different media.

LinkedIn