모바일 디바이스 체크
2022. 12. 12. 17:52ㆍSTUDY/JavaScript
모바일 디바이스를 체크하여
PC웹 환경인지, mobile 환경인지 알아낼 수 있다.
이 코드를 활용해 모바일용, 또는 PC화면용 동작을 다르게 할 수 있다.
function detectMobileDevice(agent) {
const mobileRegex = [/Android/i, /iPhone/i, /iPad/i, /iPod/i, /BlackBerry/i, /Windows Phone/i]
return mobileRegex.some(mobile => agent.match(mobile))
}
const isMobile = detectMobileDevice(window.navigator.userAgent)
if(isMobile){
// 모바일
} else {
// PC
}
'STUDY > JavaScript' 카테고리의 다른 글
| vanilla JS로 siblings 구현하기 (0) | 2023.02.13 |
|---|---|
| input 글자수 입력 후 다음 칸에 focus (0) | 2022.12.23 |
| google webFont loader (0) | 2022.12.07 |
| 모바일 환경에서의 vh (0) | 2022.11.17 |
| async await promise (0) | 2022.09.30 |