mercredi 15 avril 2015

Is there a way to check browser's current history state entries and change them?

Suppose someone comes to one of my page. I want if the visitor clicks the browser back button, instead going to the previous page, it redirects them to a feedback page asking for exit reason. Right now I'm using the following way, is that correct or is there any better way for this?



<script type="text/javascript">
window.onpopstate = function (event) {
if (typeof event.state !== 'undefined' && typeof event.state.page_name !== 'undefined' && event.state.page_name == 'third_page') {
window.location = "html3.html";
}
};

history.pushState({page_name: 'third_page'}, "page 3", "html3.html");
history.pushState({page_name: 'second_page'}, "page 2", "html2.html");
</script>

Aucun commentaire:

Enregistrer un commentaire