Solved

jquery cookies

Posted on 2011-02-11
3
450 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
3 Comments
 
LVL 20

Accepted Solution

by:
Proculopsis earned 400 total points
Comment Utility

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 100 total points
Comment Utility
>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
Comment Utility
Thanks! it worked!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now