본문 바로가기
Frontend/JavaScript

JavaScript: location 객체란?

by 코딩쥐 2024. 8. 15.

Location

Location객체는 웹 브라우저의 url과 관련된 정보를 처리하는데 사용한다. 객체가 연결된 인터넷 상에서의 위치(url)에 대한 정보 제공 및 속성을 가져올 수 있다.

속성 설명
location.href 주소영역에 참조주소를 설정하거나 URL을 반환
location.hash URL에 해시값(#에명시된값)을 반환
location.hostname URL에 호스트이름을 설정하거나 반환
location.host URL에 호스트이름과 포트번호를 가져옴
location.port URL에 포트번호를 반환
location.protocol  URL에 프로토콜을 반환
location.search URL에 쿼리(요청값)을 반환
location.reload() 새로고침이일어난다
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let url = document.createElement("a");
        url.href = "https://coding-ji.tistory.com:8080/en-US/search?q=URL#hello";

        console.log(url.href);  //https://coding-ji.tistory.com:8080/en-US/search?q=URL#hello
        console.log(url.hash); // #hello
        console.log(url.hostname); //coding-ji.tistory.com
        console.log(location.host); //127.0.0.1:5500
        console.log(url.port);  //8080
        console.log(url.protocol); //https:
        console.log(url.search); //?q=URL
    </script> 
    
</body>
</html>