Home > Back-end >  Prevent page refresh when browser back button hits
Prevent page refresh when browser back button hits

Time:06-01

I need to prevent page refresh when back button clicks in my wordpress application. I tried some solutions available online, but no use.

Here is my code in functions.php

<script>
    console.log(123)
    $(window).on('popstate', function(event) {
       console.log("pop");
    });
</script>

CodePudding user response:

window.onhashchange = function() {
   //blah blah blah
}

You can use this for more info read this answer How to Detect Browser Back Button event - Cross Browser

or you can use this

window.onbeforeunload = function() { return "Your work will be lost."; };

CodePudding user response:

You can use pageshow event to handle browser navigation through your page history

Use this code may it's Works for you

window.addEventListener( "pageshow", function ( event ) {
  var pageHistory = event.persisted || 
                         ( typeof window.performance != "undefined" && 
                              window.performance.navigation.type === 2 );
  if ( pageHistory ) {
    // Handle page restore.
    window.location.reload();
  }
});
  • Related