본문 바로가기
Java Script/Java Script

JS 예제 : 접근 기기(데스크탑 / 모바일)에 따른 배경 변경

by 워니 wony 2019. 5. 11.

 

자바스크립트에서는 해당 웹 서비스에 접근하는 사용자 브라우저에 대한 정보를 가지고 있는 객체가 있다.

 

navigator 객체

  • navigator 객체는 웹 페이지를 실행하고 있는 브라우저에 대한 정보를 가지고 있음
  • 객체 대표적인 속성
    • appName 브라우저의 이름
    • appVersion 브라우저 버전
    • userAgent 브라우저 전체적인 정보

 

 

navigator JS 예제

  • start.html에 접근하는 브라우저의 상태에 따라 페이지를 이동하게 하는 자바스크립트 예제
  • 만약 접근 브라우저가 데스크탑이면 해당 페이지에 머무르고, 모바일 페이지의 경우 mobileStart.html 페이지로 이동하게 되는 웹 서비스
  • navigator 객체의 .uwerAgent 속성으로 브라우저의 전체 정보를 확인하여 처리.

 

ㅇ start.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	var str = navigator.userAgent;
	str = str.toLowerCase();
	
	if(str.indexOf("mobile") >=0 )
	{
		location.href="mobileStart.html";
	}
</script>
</head>
<body bgcolor="pink">
	데스크탑 서비스
	<hr>
</body>
</html>

 

ㅇmobileStart.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>


</head>
<body bgcolor="yellow">
	모바일
	<hr>
</body>
</html>
반응형

댓글