반응형

브라우저의 종류, 버전 기능의 대응 상황을 알고 싶은 경우 이용

 

주요 멤버

멤버

설명

appCodeName

브라우저의 코드명

appName

브라우저명

appVersion

브라우저 버전

platform

플랫폼명

userAgent

사용자 에이전트명

javaEnabled()

Java 이용 가능한지 판단

 

navigatortest.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Navigator객체</title>
</head>
<body>
<pre>
<script type="text/javascript">
document.writeln(navigator.appCodeName);
document.writeln(navigator.appName);
document.writeln(navigator.appVersion);
document.writeln(navigator.platform);
document.writeln(navigator.userAgent);
document.writeln(navigator.javaEnabled());
</script>
</pre>
</body>
</html>
cs

결과1(Internet Explorer 11)

결과2(Google Chrome)

이전에는 navigation 객체를 이용해서 브라우저의 종류나 버전에 따라 분기 처리를 하기도 했으나

새로운 브라우저나 버전이 업데이트 때마다 분기를 추가해야 하는 불편함이 있다.

현재는 기능 테스트 방법을 활용한다.

기능테스트란 미리 호출해보고 가능한지 판단하여 동작을 결정하는 .

if(elem.attachEvent){
     //attatch
메서드를 이용할 있다면 동작할 코드
} else if(elem.addEventListener) {
     //addEventListener
메서드를 이용할 있는 경우의 코드
} else {
     //
어떤 것도 이용할 없는 경우의 코드
}

일반적으로 navigator객체를 이용하는 것은 특정 브라우저나 버전에 있는 버그에 대한 대응 코드를 기술한다.

이런 경우에도 appCode appCodeName으로는 브라우저 종류를 특정하기 어려우므로 userAgent값을 사용하도록 해야 한다.

 

반응형

'교육자료 > Javascript' 카테고리의 다른 글

18-1. DOM(Document Object Model)  (0) 2019.05.13
17-7. Screen 브라우저 객체  (0) 2019.05.13
17-4. Document 브라우저 객체  (0) 2019.05.13
17-4. Location 브라우저 객체  (0) 2019.05.13
17-3. Form 브라우저 객체  (0) 2019.05.13

+ Recent posts