skip to Main Content

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>

2

Answers


  1. 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."; };
    
    Login or Signup to reply.
  2. 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();
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search