Computer >> 컴퓨터 >  >> 프로그래밍 >> HTML

HTML DOM isDefaultNamespace() 메서드 – 기본 네임스페이스 확인 방법

HTML DOM의 isDefaultNamespace() 메서드는 지정된 네임스페이스가 해당 문서의 기본(default) 네임스페이스인지 여부를 판단하여 불리언 값(true / false)을 반환합니다.

참고: 요소 노드는 부모 노드의 네임스페이스를 상속받습니다. 따라서 XHTML 문서 내의 모든 요소는 "https://www.w3.org/1999/xhtml"이라는 namespaceURI를 갖게 됩니다.

문법(Syntax)

isDefaultNamespace() 메서드의 기본 사용 문법은 다음과 같습니다.

document.documentElement.isDefaultNamespace("nameSpaceURI")

예제(Example)

사용자가 입력한 네임스페이스 URI가 기본 네임스페이스인지 실시간으로 확인하는 예제입니다.

<!DOCTYPE html>
<html>
<head>
<title>isDefaultNamespace()</title>
<style>
    form {
        width:70%;
        margin: 0 auto;
        text-align: center;
    }
    * {
        padding: 2px;
        margin:5px;
    }
    input[type="button"] {
        border-radius: 10px;
    }
</style>
</head>
<body>
<form>
<fieldset>
<legend>isDefaultNamespace( )</legend>
<label for="textSelect">URI:
<input type="url" size="25" id="textSelect" placeholder="https://www.abc.com">
</label><br>
<input type="button" onclick="confirmNameSpace()" value="Check">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
    var divDisplay = document.getElementById("divDisplay");
    var inputText = document.getElementById("textSelect");
    divDisplay.textContent = 'NOTE: Check if specified URI is DefaultNamespace';
    function confirmNameSpace() {
        if(document.documentElement.isDefaultNamespace(textSelect.value))
            divDisplay.textContent = 'Above given namespace is default';
        else
            divDisplay.textContent = 'Above given namespace is not default';
    }
</script>
</body>
</html>

동작 방식

  • 입력 필드에 네임스페이스 URI를 입력합니다.
  • 'Check' 버튼을 클릭하면 confirmNameSpace() 함수가 실행됩니다.
  • isDefaultNamespace() 메서드의 반환값에 따라 결과 메시지가 화면에 표시됩니다.

실행 결과(Output)

위 코드를 실행하면 다음과 같은 결과를 확인할 수 있습니다.

'Check' 버튼 클릭 전:

HTML DOM isDefaultNamespace() 메서드 – 기본 네임스페이스 확인 방법

잘못된 입력값으로 'Check' 버튼 클릭 후:

HTML DOM isDefaultNamespace() 메서드 – 기본 네임스페이스 확인 방법

유효한 입력값으로 'Check' 버튼 클릭 후:

HTML DOM isDefaultNamespace() 메서드 – 기본 네임스페이스 확인 방법