javascript - Trouble with setting jQuery cookie -


first of all, sorry, see there lots of repetitions whole cookie question, yet have add 1 getting quite confused different jquery cookie plugins.

i using jquery cookie plugin v1.4.1

i implemented following code on 1 of pages (not home page!)

$(document).ready( function() {      if ($.cookie('nofadeworks')) {         // when initial effect on page enter has been seen     } else {         // functions here initial page enter effect         $.cookie( 'nofadeworks', true );         }; }); 

now works problems/questions yet:

  1. working expected in safari. when browser closed , reopened starts 0 expected , desired. yet: google chrome after close , reopen still has cookie. effect functions not enter game anymore. not desired here.

  2. it awesome if points of session tell browser forget cookie , start 0 (with effect on page) again. tried that: $.removecookie( 'nofadeworks' ); on homepage didn’t work @ all.

do explain myself? how done correctly? tried expire options without success.

thanks in advance!

note: jquery cookie v1.4.1 not accept number value: https://github.com/js-cookie/js-cookie/tree/v1.4.1. not problem.

i have created small test case jquery v2.1.4 , jquery-cookie v1.4.1 (from link above):

<!doctype html> <script src="jquery.js"></script> <script src="jquery-cookie.js"></script> <script>     $(document).ready( function() {          if ($.cookie('nofadeworks')) {             // when initial effect on page enter has been seen             alert( "has cookie!" );         } else {             // functions here initial page enter effect             $.cookie( 'nofadeworks', 'true' );             alert( "no cookie, creating..." );         };     }); </script> 

the following occurs:

  1. when enters first time, creates cookie ("no cookie, creating...")
  2. when refresh page, cookie exists ("has cookie!")
  3. if close , open browser again, says ("has cookie!"), expected result ("no cookie, creating...") because in v1.4.1 should create session cookie default , didn't specified path: "/"

if use ctrl + shift + n start window in anonymous mode cookie removed (chrome start new clean browser instance).

so here theory:

if have selected restore tabs after reopening chrome, cookie created again because chrome restoring it's window session, including cookies existed previously. if disable "restore session" feature of chrome, cookies removed once user closes browser, expected.

i didn't tested disabling feature, intuitively seems problem.

it awesome if points of session tell browser forget cookie , start 0 (with effect on page) again.

you can specify path in want cookie valid, cookie not possible read path closer root (like /):

<!doctype html> <script src="jquery.js"></script> <script src="jquery-cookie.js"></script> <script>     $(document).ready( function() {         if ($.cookie('nofadeworks')) {             // when initial effect on page enter has been seen             alert( "has cookie!" );         } else {             // functions here initial page enter effect             $.cookie( 'nofadeworks', 'true', {                 // not visible in root, in pages inside "/dir/" path                 path: "/dir/"             });             alert( "no cookie, creating..." );         };     }); </script> 

i recommend try out latest version of jquery-cookie (now js-cookie): https://github.com/js-cookie/js-cookie/releases


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -