Language/JavaScript

[JS] Object | 03. Location & History

Rayi 2024. 2. 13. 15:10

1. Location

Location은 현재 URL에 대한 정보를 제공합니다.

속성 반환값
hash <a name> 또는 href 값의 # 다음 부분
host 호스트 이름과 포트 번호
hostname 호스트 이름
href URL 전체
pathname URL 중 특정 리소스에 접근하는 경로 부분
port 포트번호
protocol 통신방식
search href 값의 ? 다음 부분
target 타깃 프레임의 이름

 

메소드 매개변수 기능 반환값
assign(url) str url : URL 주소 URL의 HTML 문서 읽기 HTML 문서
reload()   현재 문서 다시 읽기 HTML 문서
replace(url) str url : URL 주소 history 객체의 URL을 현재 주소로 대체
※ 이전의 주소는 history에 남지 않음
HTML 문서

 

var count = 10;

function reloadPage() {
    count--;

    if (count == 0){
        document.write("reload <br />");
        window.location.reload();
    }
    else{
        document.write("waiting..."+count+"s <br />");
    }
}

window.setInterval('reloadPage()', 1000);

 

2. History

History는 과거의 URL 방문 기록에 접근할 때 사용됩니다.

가장 대표적인 예시로 뒤로가기가 있습니다.

메소드 매개변수 기능 반환값
back()   뒤로가기  
forward()   앞으로가기  
go(index) int index : 이동 인덱스 index 만큼 페이지 이동
index가 음수일 때는 뒤로
 

 

var count = 5;

function reloadPage(){
    count--;
    if(count == 0){
        document.write('go back to previous page <br />');
        window.history.go(-1);
    }
    else{
        document.write(count + 'second left to reload<br />');
    }
}

window.setInterval('reloadPage()', 1000);
728x90