Programming/WEB
[js] 브라우저별 동작
코딩의성지
2022. 5. 19. 18:26
웹 작업을 하다보면 브라우저 별로 다르게 작업해야하는 경우가 생긴다.
이는 아래와 같은 JS 함수를 작성해서 이용할 수 있다.
function actionPerBrowser() {
const agent = window.navigator.userAgent.toLowerCase();
switch (true) {
case agent.indexOf("edge") > -1: //엣지
//Action
break;
case agent.indexOf("edg/") > -1: // 크롭기반 엣지
//Action
break;
case agent.indexOf("opr") > -1: // 오페라
//Action
break;
case agent.indexOf("chrome") > -1 && !!window.chrome: //크롬
//Action
break;
case agent.indexOf("trident") > -1 && -1: // 익스플로러
//Action
break;
case agent.indexOf("firefox") > -1: // 파이어폭스
//Action
break;
case agent.indexOf("safari") > -1: //사파리
//Action
break;
default:
//Action
break;
}
}
끝.
반응형