0

I have an issue . I am trying to clear a cookie value on tab close. and i have created a function But it clears value on page reload also . I also tried this solution stack-overflow question

function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
}
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires+"; path=/";
}
function unloadPage()
{ 
    var check=getCookie('mycookie')
    if(check)
    setCookie('mycookie','','-2'); 
}
$(document).ready(function() {
    window.onbeforeunload = unloadPage;
});

Function is working but problem is it is also clearing cookie on page reload . but i only want to clear cookie if user close the window.

Thanks.

Community
  • 1
  • 1
Manoj Dhiman
  • 4,994
  • 6
  • 25
  • 63

0 Answers0