Solved

jquery cookies

Posted on 2011-02-11
3
452 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
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 100 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use embargo Date/time in php 29 38
Problem to refer to value 8 48
jQuery delay animation 3 29
Diminish Pop-up  in 3 seconds 7 17
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

770 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