Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jquery cookies

Posted on 2011-02-11
3
Medium Priority
?
458 Views
Last Modified: 2012-05-11
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
Comment
Question by:glepiza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 1600 total points
ID: 34873788

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
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 400 total points
ID: 34873863
>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
 

Author Closing Comment

by:glepiza
ID: 34874070
Thanks! it worked!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question