뒤로가기로 왔을 경우

2022. 5. 17. 18:37웹 프로그래밍/JavaScript

 

//뒤로가기로 왔을 경우 페이지 새로고침
if (window.performance) {
     var navEntries = window.performance.getEntriesByType('navigation');
     if (navEntries.length > 0 && navEntries[0].type === 'back_forward') {
          console.log('As per API lv2, this page is load from back/forward');
					location.reload();
     } else if (window.performance.navigation
          && window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD) {
          console.log('As per API lv1, this page is load from back/forward');
					location.reload();
     } else {
          console.log('This2 is normal page load');
     }
} else {
     console.log("Unfortunately, your browser doesn't support this API");
}

//아이폰으로 뒤로가기로 왔을 경우, 아래처럼 새로고침
//Javascript
window.onpageshow =  function(event) { // BFCahe
    if (event.persisted) {
        window.location.reload();
    }else{} //새로운페이지
}
728x90