개발자의 끄적끄적

[javascript/jquery] javascript http to https redirect example 본문

개발/javascript & jquery

[javascript/jquery] javascript http to https redirect example

효벨 2022. 8. 6. 01:00
728x90
반응형

[javascript/jquery] javascript http to https redirect example

 

 

javascript 에서

 

http 로 접속된 도메인을 https 로 리다이렉트 시켜야 하는 경우가 있습니다.

 

그럴때는 아래 스크립트를

 

이용하시면 됩니다!

 

if (document.location.protocol == 'http:') {
    document.location.href = document.location.href.replace('http:', 'https:');
}

 

위 내용을 보시면 아시겠지만,

 

document.location.protocol 을 출력해보면

 

http: 나 https: 가 출력됩니다.

 

이것을 이용해서 접속 url 을 체크하고,

 

location.href 로 https 로 변경된 도메인으로 리다이렉트 시키는 방법입니다!

 

 

참고들 하세요!

반응형
Comments