본문 바로가기
  • 그냥 하자
Web Programing

모바일 웹사이트(mobile website) 제작 관련 userAgent 를 통한(device 구분) 원하는 주소로 포워딩 [출처] 모바일 웹사이트(mobile website) 제작 관련 userAgent 를 통한(device 구분) 원하는 주소로 포워딩|작성자 문피쉬

by Mash 2011. 2. 23.
반응형

인터넷 웹사이트에 방문하였을 경우 해당 device를 구분하여 pc 용 웹사이트를 띄울 것인가

그렇지 않으면 모바일 웹사이트를 띄울 것인가 하는 구분이 필요 합니다. 해당 경우를 처리하기 위하여는 아래와 같이 자바스크립트 구문을

사용하여 처리하면 됩니다.

 

<script>

var browser = navigator.userAgent.toLowerCase();

var user_browser = (browser.indexOf('iPhone')!=-1);

if ( user_browser ) {

 document.location.href = "URL what you want";

}

</script>

 

---------------------------------------------------

 

위의 예제는 iphone 을 색출 하였습니다만. (기본적인 구성이 되겠네요 / 아래는 좀더 세밀하게 구분하였습니다)

 

<script>

if (navigator.userAgent.match(/iPad/) == null && navigator.userAgent.match(/iPhone|Mobile|UP.Browser|Android|BlackBerry|Windows CE|Nokia|webOS|Opera Mini|SonyEricsson|opera mobi|Windows Phone|IEMobile|POLARIS/) != null){

 location.href = "mobile_index.html";  //모바일용 인덱스파일을 띄어라

} else {

location.href = "index.htm"  //PC 용 인덱스 파일을 띄어라

}

</script>


반응형