javascript - jQuery: remove a cookie when closing the browser (session cookie) -
it sounds simple , think should simple, somehow don't work...
i want set cookie using javascript , cookie should removed when user quits browser. setting cookie , getting value not problem. when quit browser , reopen it, cookie still there (firefox).
i use jquery , cookie-plugin.
here test code:
$(document).ready(function(){ $('#output').append( '<li>initialize...</li>' ); var $cookieval = $.cookie('testcookie'); $('#output').append( '<li>check cookie...</li>' ); if(!$cookieval) { $('#output').append( '<li>set cookie...</li>' ); $.cookie('testcookie', 'eat cookies', { path: '/' }); //console.log( $.cookie() ); } else { $('#output').append( '<li>cookie set...</li>' ); $('#output').append( '<li>cookie value: '+$.cookie('testcookie')+'</li>' ); } });
please find working example @ jsfiddle.
i beginning wonder if testing method might problem here. so, going write in specific way.
actual answer: browser setting
in firefox, options>general>when firefox starts>"show windows , tabs last time" going preserve previous session. change setting see indeed working supposed to. firefox prolonging session. further information, see "bug": http://bugzilla.mozilla.org/show_bug.cgi?id=530594
there similar settings in browsers behave same way.
original answer:
i created fiddle, http://jsfiddle.net/8ahg2/ uses document.cookie rather jquery cookie plugin. here how test this. (source below)
- copy following url clipboard:
http://fiddle.jshell.net/8ahg2/show/
- completely close browser of choice - should browser independent.
- open browser, paste url. first time should say:
check cookie... set cookie...
- refresh page, notice should value of cookie ("test")
- close browser completely again.
- navigate url should still in clipboard. *do not refresh page on first view, should again '
check cookie... set cookie...
'
js
$(document).ready(function () { $('#output').append('<li>initialize...</li>'); //this regex gets "name" cookie out of string of cookies this: "name=test;var2=hello;var3=world" var cookieval = document.cookie.replace(/(?:(?:^|.*;\s*)name\s*\=\s*([^;]*).*$)|^.*$/, "$1"); $('#output').append('<li>check cookie...</li>'); if (!cookieval) { $('#output').append('<li>set cookie...</li>'); document.cookie = "name=test"; } else { $('#output').append('<li>cookie set...</li>'); $('#output').append('<li>cookie value: ' + cookieval + '</li>'); } });
Comments
Post a Comment