I have a problem with detecting the browser back button event. I managed to call script when back-button is clicked, but the problem lies in the hashtag.
The script works when the url doesn't contain a hashtag. However when the hashtag character is in the url, the script automatically fires even if the back button wasn't clicked.
window.onload = function () {
if (typeof history.pushState === "function") {
history.pushState("jibberish", null, null);
window.onpopstate = function () {
history.pushState('newjibberish', null, null);
alert(' It Works !')
};
} else {
var ignoreHashChange = true;
window.onhashchange = function () {
if (!ignoreHashChange) {
ignoreHashChange = true;
window.location.hash = Math.random();
alert('else!');
} else {
alert('else in else!');
ignoreHashChange = false;
}
};
}
}
How can I modify the script to run only when the back button is clicked?