On Ready of the screen i used the javascript to prevent navigation from current to previous screen while clicking back arrow in the browser which is working as expected but screen is reloaded again which should be avoided so that data action should not trigger ,how to resolve this
window.history.pushState(null, null, window.location.href);window.onpopstate = function () { window.history.go(0); };
The code you have is causing the page to reload because the window.history.go(0) line is triggering a reload of the current page.
If you want to prevent navigation to the previous screen without triggering a reload, you can try this:
window.history.pushState(null, null, window.location.href);
window.onpopstate = function (event) {
};
Hi Mohd Anees Mansoori,
I tried but the current screen is reloading
An alternative approach you can try is using the window.location.replace() method, which can help prevent navigation to the previous page while maintaining the current state of the page.
window.location.replace(window.location.href);
Thanks.
Still facing the same issue
Hey R.kar,
perhaps this piece of javascript might help you
// Disable the default behavior of the back button
window.onbeforeunload = function() {
history.pushState(null, null, document.URL);
// Handle the popstate event when the back button is clicked
window.onpopstate = function(event) {
// Perform actions to update the page content if needed
// For example, you can update the UI or fetch new content using AJAX.
console.log('Back button was clicked');
//Explanation
In this code, the onbeforeunload event prevents the default behavior of the back button and instead pushes a new state into the history with the same URL as the current page. This effectively "resets" the history, allowing you to use the onpopstate event to handle the back button click without causing a page reload.
Kind regards,Marcel