EzDevInfo.com

jquery-cookie

No longer maintained, superseded by JS Cookie: js-cookie/js-cookie · GitHub js-cookie - a simple, lightweight javascript api for handling cookies

How to unset a jquery tab cookie?

Hi i have the following code

 var $tabs = $("#tabs").tabs({
      cookie: { expires:1 }
  });

this very nice. But how can i unset this cookie? something like

$( "#tabs" ).tabs( "option", "cookie", { expires: 0 } );

Any suggestion?


Source: (StackOverflow)

Create a cookie if (and only if) it doesn't already exist

I want to:

  1. Check to see if a cookie with name of "query" exists
  2. If yes, then do nothing
  3. If no, create a cookie "query" with a value of 1

Note: I am using jQuery 1.4.2 and the jQuery cookie plugin.

Does anyone have any suggestions as to how I can do this?


Source: (StackOverflow)

Advertisements

How do I store an array of objects in a cookie with jQuery $.cookie()?

I have a list of javascript objects:

var people = [
   { 'name' : 'Abel', 'age' : 1 },
   { 'name' : 'Bella', 'age' : 2 },
   { 'name' : 'Chad', 'age' : 3 },
]

I tried to store them in a browser cookie with jQuery $.cookie():

$.cookie("people", people);

I then retrieve this cookie and then try to push another object into it:

var people = $.cookie("people");
people.push(
    { 'name' : 'Daniel', 'age' : 4 }
);

However, this does not work; I analyzed this code in Firebug, and Console noted that people was a string ("[object Object],[object Object],[object Object]") and that the push function did not exist.

What is going on? What is the proper way to store and retrieve a list of objects?


Source: (StackOverflow)

Why does jQuery cookie not actually set a cookie?

I am developing an application using jQuery that uses cookies. Right now, it is located at application.html on my PC desktop.

However, I cannot store and retrieve a cookie. I had included jquery-1.7.1.min.js, json2.js, and jquery.cookie.js in my HTML file in that order.

Here is how I am storing a cookie to last for 7 days:

$.cookie("people", JSON.stringify(people_obj_array), {expires: 7});

The global array people_obj_array looks like

[
        {
            "name": "Adam",
            "age": 1,
        },
        {
            "name": "Bob",
            "age": 2,
        },
        {
            "name": "Cathy",
            "age": 3,
        },
    ]

When I test JSON encryption with alert(JSON.stringify(people_obj_array)), it looks fine:

JSON test

However, when I retrieve this cookie via:

alert($.cookie("people"));

before even refreshing the page, an alert pops up that reads "null." Shouldn't the text be the alert JSON string? Am I using the JQuery cookies library correctly?


Just to clarify, here is how I am testing:

$.cookie("people", JSON.stringify(people_obj_array), {expires: 7}); // store
alert($.cookie("people")); // attempt to retrieve

I have Firebug, and I am willing to do some Console tests.


Source: (StackOverflow)

jquery cookie set value to boolean true

I am using this jquery.cookie plugin and I need to set value to TRUE or NULL/FALSE.

I am trying to do it like this: $.cookie('ff', true, { expires: 30, path: '/' }); but it sets the value to string and not boolean.

Any ways of fixing this?


Source: (StackOverflow)

jQuery tabs with cookie - If cookie exists?

I have this code:

jQuery(document).ready(function($) {
        $( "#tabs" ).tabs({
            collapsible: true,
            fx: { height: 'toggle', duration: 'fast'},
            cookie: { expires: 30 }
        });
    });

I use jQuery tabs with a cookie set. If no cookie is set I want to hide the tabs. I have the jquery.cookie plugin installed that was required.

My question

How can I check if the tabs cookie is set or not?


Source: (StackOverflow)

JQuery cookie extension will set a cookie with a path but will not read it

First, set a cookie:

jQuery.cookie('monster', 'big', { path : '/sesame/'});

Next, try to read it:

jQuery.cookie('monster');

Firefox tells me that the cookie has indeed been set. The value is big and the path is /sesame/. And yet when I tried to read the cookie it wouldn't work.

Alternate version of the question: How do I specify the path when reading a cookie?

As an experiment I used the following syntax but it sets a cookie rather than read one.

$.cookie('cookie_name', { path: '/path/' });

Source: (StackOverflow)

Is it possible for jQuery cookies to expire like session variables?

I know this question has been asked a thousand times but none of the answers really give me what I'm looking for. I am using jQuery cookie to store some information, but I want them to expire when the browser closes. window.unload is not a viable option (read: doesn't work.)

My question is this: is it actually possible to have the cookies set to expire when the browser closes, like a session? If so, does anyone know how?

Update: Turns out it's quite simple, instead of passing expiry: 0 (which didn't work), just don't pass any expiry at all. Well, this explains why there wasn't many questions.

Thanks all.


Source: (StackOverflow)

Is using the jQuery Cookie plugin a valid way of testing to see if cookies are enabled?

I have a site that we require the user to have enabled JavaScript and cookies before they can login to the site. (The JS part is done and works perfectly.) At the moment, we have been been setting a cookie and then redirect the user to another page (in PHP). This has worked fine, but now we have a bunch of people that have bookmarked the page we are redirecting to, which of course doesn't have the cookie set and therefore doesn't allow them to login.

So I'm trying to find another solution to check for the cookie and I'm thinking of using the jQuery Cookie plugin. I'm wondering if it's compatible in all browsers (when JS is enabled of course)?


Source: (StackOverflow)

How to read the cookie creation date (not expiration)

Is there a way to store in a variable a cookie creation date? I'm using the jquery.cookie plugin. If there is not a way, I'm thinking about store in the cookie, as value, the actual time/date. It could be a solution.

Thanks.


Source: (StackOverflow)

jQuery Cookies not working as expected in IE

I have Used jQuery Cookies to set a cookie variable as below

for(ck=1;i<= $.cookie('ck'); ck++){
   $.cookie('Answer'+answer, answer);
   $.cookie('questions'+ck, $('#quid').text());
   $.cookie('Answer'+ck, $('#'+answer).val());
   $.cookie('status'+ck, statuss);
   $.cookie('correctans'+ck, base64_decode(correctans)); 

}

It works Perfectly in chrome and firefox. it also works correctly in IE if the values of $.cookie('ck') is less then 9. if this is greater then 9, cookies of answer9 will be set and answer1 will be unset in IE.What have to do to resolve this?


Source: (StackOverflow)

jquery, delete cookies

I want to use JQuery to delete cookies ; I tried this

$.cookie('name', '', { expires: -1 });

Then I refresh the page and the cookie is still there :

alert('name:' +$.cookie('name'));

Why? Thanks


Source: (StackOverflow)

How do I set a cookie to expire after 1 minute or 30 seconds in Jquery?

How do i set my cookie to expire after 30 sec or 1 m ? this is my code :

$.cookie('username', username, { expires: 14 });  // expires after 14 days

Source: (StackOverflow)

jQuery $.cookie not reading cookie set by server response

$.cookie is not reading cookies that have been previously set by the server response.

I can read cookies that have been set with $.cookie().

I can see all domain cookies set with Firefox's Web Developer add on.
Also, the server side can see the request cookies, so the browser definitely has them.

I have experimented with expiry times with no avail.


Source: (StackOverflow)

'JSON' and 'jQuery' still undefined in Internet Explorer 7 and 8

I got these three popular scripts included between my <head> tags

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/json3/3.2.6/json3.min.js"></script>
<script type="text/javascript" src="/js/jquery.cookie.js"></script>

When I check for errors in IE7 and 8 I get the following:

  1. 'JSON' is undefined. (IE7)
  2. 'jQuery' is undefined. (IE7 and IE8)
  3. Object doesn't support this property or method. (IE8)

Can anybody tell me what is the cause of this, because I clearly am including those on my page, yet still come up as undefined. I have nothing else yet on my page besides these includes.

How can I fix this?


Source: (StackOverflow)