Search code examples
javascriptasp.netpage-refresh

To check if page is refreshing or navigating to another page


I want to delete all the cookies if my page is navigating to other page or closing, else I want to let the page refresh normally. The reason behind to do so is Clearing cookie for tabs.

So my Question is:
How will we be able to know that either the page is refreshing or navigating or closing?

I tried using

window.onbeforeunload = function() {
    var old_url= window.location.href;
    var new_url=  /* I dont know how to get new URL here */;
    if(old_url == new_url){
      return true;
    }
    else {
      return false;
    }
}

Its not working :(
Is there any other way to do it?


Solution

  • I declared a global variable in javascript.

    <script type="text/javascript">
      var is_refresh_true = true;
    </script>
    

    And whenever I was trying to refresh my page using javascript i just changed the variable value to false
    For ex:

    <script type="text/javascript">
       function Callme(){  
          is_refresh_true = false;
          window.location.href = window.location.href;
        }
    </script>
    

    Also onUnload event of body:

     function DeleteCokies() {            
         if (is_refresh_true) {
                deleteAllCookies();
         }
     }
    

    Its not a perfect solution but works for me.
    Still I am waiting for perfect answer. Please reply if anyone finds it.