Prasad Jadhav
Prasad Jadhav

Reputation: 5208

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?

Upvotes: 1

Views: 2265

Answers (3)

Lucky
Lucky

Reputation: 195

var confirmOnPage = function () {

   function del_cookie(name)
   {
       document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
   }

};

window.onbeforeunload = confirmOnPage;

Upvotes: 0

techBeginner
techBeginner

Reputation: 3850

If you don't set any Expiration for the cookie, it will be cleared with the session(ie., when you close the browser)..

If you are navigating to other pages, I don't see any problem with those cookies on other pages.. and suppose if you are coming back to the same page again, it will show the same tab..

Upvotes: 0

Prasad Jadhav
Prasad Jadhav

Reputation: 5208

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.

Upvotes: 1

Related Questions