jquery - Check for Cookie, if it doesn not exist run script -
i'm struggling use of cookies on html page. have script check see if user has cookie, if not call script open model window
<script type="text/javascript"> jquery(document).ready(function() { jquery("#mymodal").reveal(); }); </script
the window contains "yes" , "no" buttons, if user selects "yes" save cookie modal doesn't open on future visits page, if user selects "no" direct them page.
any appreciated!!
1) use jquery cookie library ease of use. https://github.com/carhartl/jquery-cookie
2) check if cookie exists , handle accordingly
if ($j.cookie("nameofyourcookie") == null) { //do stuff if cookie doesn't exist set cookie value of 1 $j.cookie("nameofyourcookie", 1, {expires: 10, path:'/'}); } else { //they have cookie alert ('you have cookie name nameofyourcookie'); }
* edit *
thought might edit answer more appropriate question.
<button class="yes">yes</button> <button class="no">no</button> var $j = jquery.noconflict(); $j(document).ready(function(){ //show modal on page load $j("#mymodal").show(); $j('.yes').click(function(){ $j("#mymodal").hide(); $j.cookie("yesbuttoncookie", 1, {expires: 10, path:'/'}); alert ('you have clicked yes, cookie has been dropped'); }); if ($j.cookie("yesbuttoncookie") == 1){ // don't show div $j("#mymodal").hide(); alert ('cookie found, cant see mymodal'); } if ($j.cookie("yesbuttoncookie") == null){ // cookie not found alert ('cookie not found, can see mymodal'); $j("#mymodal").show(); } $j('.clearcookie').click(function(){ $j.cookie("yesbuttoncookie", null); alert('cookie cleared'); }); });
Comments
Post a Comment