Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining navigation type from PerformanceNavigationTiming

I used to determine if user had come by clicking the back button in the previous page using window.performance.navigation.type like

if (window.performance.navigation.type === 2) {
    window.location.reload()
}

I saw that this property has been deprecated and succeeded by Navigation Timing Level 2. How can I mimic the behavior of performance.navigation api by this?

like image 804
desertSniper87 Avatar asked Dec 04 '18 12:12

desertSniper87


1 Answers

I should preface this by saying that I am not JavaScript pro. This is my approach for utilizing Navigation Timing Level 2.

if (String(window.performance.getEntriesByType("navigation")[0].type) === "back_forward") {
    window.location.reload()
}

window.performance.getEntriesByType("navigation") returns a list of PerformanceEntry objects every mark set. If you haven't set any marks, this list contains a single object that has the navigation information for the window.

like image 117
CraigF Avatar answered Sep 20 '22 15:09

CraigF