Javascript : Microsoft OS & Internet Explorer Agent Check

2009.07.20 12:26Programming/JavaScript

 

 

Web Page에서 브라우저와 OS에 따라 다른 조치를 해야 하는 경우가 있습니다.

 

그래서 브라우저에서 받아올 수 있는 User Agent 값을 분석해서 이에 이용하게 되죠.

 

다음과 같이 값을 체크하는 부분을 공통으로 작성하여 가져다가 쓰면 좋을 것 같네요.

 

 

 

 

테스트 페이지(Test page): BrowserTest.html

 

<html>

<head>

<title>Browser Agent Test</title>

<script type="text/javascript">

   

function BrowserIs()

{

var agt = navigator.userAgent.toLowerCase();

this.osver = 1.0;

 

if (agt)

{

var stOSVer = agt.substring(agt.indexOf("windows ") + 11);

this.osver = parseFloat(stOSVer);

}

 

this.major = parseInt(navigator.appVersion);

this.nav = ((agt.indexOf('mozilla')!=-1)&&((agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible')==-1)));

        this.nav2 = (this.nav && (this.major == 2));

this.nav3 = (this.nav && (this.major == 3));

this.nav4 = (this.nav && (this.major == 4));

this.nav6 = this.nav && (this.major == 5);

this.nav6up = this.nav && (this.major >= 5);

this.nav7up = false;

   

if (this.nav6up)

{

var navIdx = agt.indexOf("netscape/");

if (navIdx >=0 )

this.nav7up = parseInt(agt.substring(navIdx+9)) >= 7;

}

   

this.ie = (agt.indexOf("msie")!=-1);

this.aol = this.ie && agt.indexOf(" aol ")!=-1;

   

if (this.ie)

{

var stIEVer = agt.substring(agt.indexOf("msie ") + 5);

this.iever = parseInt(stIEVer);

this.verIEFull = parseFloat(stIEVer);

}

else

this.iever = 0;

   

this.ie3 = ( this.ie && (this.major == 2));

this.ie4 = ( this.ie && (this.major == 4));

this.ie4up = this.ie && (this.major >=4);

this.ie5up = this.ie && (this.iever >= 5);

this.ie55up = this.ie && (this.verIEFull >= 5.5);

this.ie6up = this.ie && (this.iever >= 6);

this.ie7up = this.ie && (this.iever >= 7);

this.ie8up = this.ie && (this.iever >= 8);

this.win16 = ((agt.indexOf("win16")!=-1)

|| (agt.indexOf("16bit")!=-1) || (agt.indexOf("windows 3.1")!=-1)

|| (agt.indexOf("windows 16-bit")!=-1) );

this.win31 = (agt.indexOf("windows 3.1")!=-1) || (agt.indexOf("win16")!=-1) ||

(agt.indexOf("windows 16-bit")!=-1);

this.win98 = ((agt.indexOf("win98")!=-1)||(agt.indexOf("windows 98")!=-1));

this.win95 = ((agt.indexOf("win95")!=-1)||(agt.indexOf("windows 95")!=-1));

this.winnt = ((agt.indexOf("winnt")!=-1)||(agt.indexOf("windows nt")!=-1));

this.win32 = this.win95 || this.winnt || this.win98 ||

((this.major >= 4) && (navigator.platform == "Win32")) ||

(agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1);

this.os2 = (agt.indexOf("os/2")!=-1)

|| (navigator.appVersion.indexOf("OS/2")!=-1)

|| (agt.indexOf("ibm-webexplorer")!=-1);

this.mac = (agt.indexOf("mac")!=-1);

this.mac68k = this.mac && ((agt.indexOf("68k")!=-1) ||

(agt.indexOf("68000")!=-1));

this.macppc = this.mac && ((agt.indexOf("ppc")!=-1) ||

(agt.indexOf("powerpc")!=-1));

this.w3c = this.nav6up;

}

   

function ObjectStripShow(obj, outputDivId)

{

var outputStr = "";

 

for (var i in obj) outputStr += i + " = " + eval('obj.' + i) + "<br/>\n";

if (outputDivId == null || document.getElementById(outputDivId) == null)

document.write(outputStr);

else

document.getElementById(outputDivId).innerHTML = outputStr;

}

   

window.onload = function()

{

var bi = new BrowserIs();

ObjectStripShow(bi, 'output');

};

   

</script></head>

<body>

<div id="output">

</div>

</body>

</html>


 

 

 

 

결과(Result ):

 

osver = 5.2
major = 4
nav = false
nav2 = false
nav3 = false
nav4 = false
nav6 = false
nav6up = false
nav7up = false
ie = true
aol = false
iever = 7
verIEFull = 7
ie3 = false
ie4 = true
ie4up = true
ie5up = true
ie55up = true
ie6up = true
ie7up = true
ie8up = false
win16 = false
win31 = false
win98 = false
win95 = false
winnt = true
win32 = true
os2 = false
mac = false
mac68k = false
macppc = false
w3c = false