90
Views
5
Comments
How to restrict screen loading

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); };

2025-09-21 06-51-05
Mohd Anees Mansoori

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) {

    window.history.pushState(null, null, window.location.href);

};


UserImage.jpg
karthimani R

Hi Mohd Anees Mansoori, 

I tried but the current screen is reloading

2025-09-21 06-51-05
Mohd Anees Mansoori


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.history.pushState(null, null, window.location.href);

window.onpopstate = function (event) {

    window.location.replace(window.location.href);

};

Thanks.

UserImage.jpg
karthimani R

Hi Mohd Anees Mansoori, 

Still facing the same issue

2023-05-27 19-28-02
marz1966

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


Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.