When the user goes history-back-1...how do I detect that? And then, alert "the user clicked back!"
Using binds (and jQuery preferably)
When the user goes history-back-1...how do I detect that? And then, alert "the user clicked back!"
Using binds (and jQuery preferably)
You generally can't (browser security restriction). You can tell if the user navigates away from the page (onbeforeunload, onunload fire) but you can't tell where they went unless you've set up your page to allow it.
HTML5 introduces the HTML5 History API; in conforming browsers, the onpopstate event will fire if the user navigates back to an earlier "page" on your site.
try:
window.onbeforeunload = function (evt) {
var message = 'Are you sure you want to leave?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
window.onpopstate=function()
{
alert("Back/Forward clicked!");
}
Following are the steps to detect back button click:
$('body').on('mousedown', 'on all li');
IF variable changes to true it means list clicked otherwise back button.
This work in my use case. This solution may help others because it depends on app design.
hide loading onload or when using back button:
jQuery(document).ready(function ($) {
$.minicart.hideLoading();
});
window.addEventListener('load', function () {
jQuery(document).ready(function ($) {
$.minicart.hideLoading();
});
});
window.onload = function () {
$.minicart.hideLoading();
}
On the page you are looking at, you can add this piece of code to the onLoad
event to move them back the page they were on.
if(history.length>0)history.go(+1)
If you want the alert then make it
if(history.length>0)alert("the user clicked back!")
history.length > 0
will give you true even when its a brand new page. It gives you the entire length of history including the backwards and forwards pages. –
Ferritin © 2022 - 2024 — McMap. All rights reserved.