VScode/JavaScript

[JavaScript] navigator

hvoon 2022. 9. 9. 14:16

-웹페이지를 실행하고 있는 브라우저에 대한 정보를 가짐

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>NavigatorObject.html</title>
    <script type="text/javascript">
        var output='';
        for(var key in navigator){
            output+='<h3>'+key+':'+navigator[key]+'</h3>';
        }
        output+='<h3>'+navigator.userAgent+'</h3>';
        document.write(output);
 /* navigator가 제공하는 핵심기능: 접속하는 디바이스(엄밀히 말하면 운영체제)에 따라 표시되는 페이지
   (엄밀히 말하면 표시될 페이지의 경로를 달리해서)를 표시함 */
       if( (navigator.userAgent.match(/iPhon/i)) || (navigator.userAgent.match(/iPad/i))
           || (navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/Windows CE/i))
           || (navigator.userAgent.match(/Symbian/)) || (navigator.userAgent.match(/BlackBerry/i))
           || (navigator.userAgent.match(/Android/i)) ){
            // 스마트 기기용 페이지로 이동
            location.href='mobile.html';
        } else{
            // PC용 페이지로 이동
            location.href='desktop.html';
        }
    </script>
</head></html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>desktop.html</title>
</head>
<body>
    <h1>데스크탑에서 접속</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h1>모바일에서 접속</h1>
</body>
</html>

모바일 접속은 Android studio를 설치하여 확인 가능

http://해당IP주소:포트번호