1

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?

Community
  • 1
  • 1
Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
  • Could you not store the values in a mechanism that automatically gets lost in the page unload. Such as viewState or hidden fields? – Daniel Casserly Sep 17 '12 at 13:32
  • Thanks Daniel. Please check my answer and let me know if there are any flaws in it. – Prasad Jadhav Sep 17 '12 at 14:27
  • why do you want to clear the cookies on navigating to other page? – techBeginner Sep 17 '12 at 14:40
  • bcoz the tabs are dynamic...and hence the no. of tabs and tab names are changing along with content and jquery cookies store the cookies as **ui-tabs-1=2** where 2 is tab position form left. Hence if my tab structure changes it always opens the second tab. – Prasad Jadhav Sep 18 '12 at 05:15

3 Answers3

1

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.

Prasad Jadhav
  • 5,090
  • 16
  • 62
  • 80
0

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..

techBeginner
  • 3,792
  • 11
  • 43
  • 59
0
var confirmOnPage = function () {

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

};

window.onbeforeunload = confirmOnPage;

Lucky
  • 197
  • 1
  • 1
  • 9