Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

jquery cookies

Hello Experts,

I would really appreciate if someone can please help me to solve the following issue.

I am creating a on/off button using jquery. When the user clicks ON, the page should refresh and set the cookie and the cookie should stay there until the user closes the browser windows. I am using jquery.cookies.js library.

Can someone please guide me on this? I've been checking a bunch of tutorials and so far my script only sets the cookie (when I refresh the page manually), but I don't see it getting deleted and the page does not refresh automatically. Someone told me to use https request, and well... no idea how to do that....

Any kind of guidance would be greatly appreciate it

Thanks in advance,

winter

PS: this is my script:
    var active = 'active';
    $('#offState a').live('click', function (e) {
        e.preventDefault();
        $.cookie(active, "activemodal", { expires: 7 });

    });
    $('#onState a').live('click', function (e) {
        e.preventDefault();

        $.cookie(active, null);

    });
0
glepiza
Asked:
glepiza
2 Solutions
 
ProculopsisCommented:

Your code seems fine, perhaps it's cacheing problem - the following works:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26815591.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
<script src="http://www.shopdev.co.uk/blog/jquery.cookie.js" type="text/javascript"></script>

<script type="text/javascript"> 

$(document).ready(function() {

  var active = 'active';
  $("#active").text( $.cookie( active ) );

  $('#offState a').live('click', function (e) {
    e.preventDefault();
    $.cookie(active, "activemodal", { expires: 7 });

  });
  $('#onState a').live('click', function (e) {
    e.preventDefault();

    $.cookie(active, null);

  });

  $("#refresh").click( function() {
    document.location.reload();
  });

});

</script>

</head>
<body>

Cookie activemodal: <span id="active"></span>

<fieldset id="onState"><legend>State on</legend>
<a href="#">On</a>
</fieldset>

<fieldset id="offState"><legend>State off</legend>
<a href="#">Off</a>
</fieldset>

<input id="refresh" type="button" value="Refresh" />

</body>
</html>

Open in new window

0
 
leakim971PluritechnicianCommented:
>When the user clicks ON, the page should refresh and set the cookie

If the page is refreshed the running of your script may be broken
Should be :
- set the cookie
- refresh the page

To clear the cookie try this : $.cookie('cookie', '', { expires: -1 });

A good link : http://www.zorrito.com/cookie-jquery-pour-style-switcher/
0
 
glepizaAuthor Commented:
Thanks! it worked!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now