개발/HTML, JS
프로토콜, 도메인, 포트, 파일(페이지)명 구하기 예제
공유민
2018. 1. 15. 16:15
안녕하세요. 공유민입니다.
스크립트단에서 현재 접속한 페이지의 프로토콜, 도메인 ,포트, 파일명, 파라미터를 구하는 예제 코드를 준비했습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!DOCTYPE html> <html> <head> <script type="text/javascript"> alert("protocol : " + location.protocol); // 프로토콜 (http:, file:, ...) alert("domain : " + location.host); // 도메인(포트번호 포함) (www.blog.com:80) alert("port : " + location.port); // 포트번호 (80) alert("path : " + location.pathname.substring(1)); // '/' 이후 주소 (test/sample.html) alert("filename : " + location.pathname.substring(location.pathname.lastIndexOf("/") + 1)); // 파일명 (sample.html) alert("parameter : " + location.search); // 파라미터 (?test=ok&aaa=bbb) </script> </head> <body> </body> </html> | cs |
감사합니다.