Is the cookie perpetually non-existent?

I am trying to determine whether or not a cookie exists

However, whenever I check for it, it always returns null

if ($.cookie('cookieCreditDash') == null) {

   //CODE ALWAYS ENTER HERE... :(
            alert("no cookie");    
            $.getJSON(url, function(data) {
                saveCreditCookie(data);
            });
} else{
            alert("with cookie");
            var data =  JSON.parse($.cookie("cookieCreditDash"));
}

function saveCreditCookie(jsonData){
    var date = new Date();
    date.setTime(date.getTime() + (30 * 60 * 1000));
    $.cookie("cookieCreditDash", JSON.stringify(jsonData), {expires: date});
 }

WHYYY>??

BTW, I utilized the following plugin:

(function ($, document, undefined) {
        var pluses = /\+/g;
        function raw(s) {
            return s;
        }
        function decoded(s) {
            return decodeURIComponent(s.replace(pluses, ' '));
        }
        var config = $.cookie = function (key, value, options) {
            // write
            if (value !== undefined) {
                options = $.extend({}, config.defaults, options);
                if (value === null) {
                    options.expires = -1;
                }
                if (typeof options.expires === 'number') {
                    var days = options.expires, t = options.expires = new Date();
                    t.setDate(t.getDate() + days);
                }
                value = config.json ? JSON.stringify(value) : String(value);
                return (document.cookie = [
                    encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
                    options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                    options.path    ? '; path=' + options.path : '',
                    options.domain  ? '; domain=' + options.domain : '',
                    options.secure  ? '; secure' : ''
                ].join(''));
            }

            // read
            var decode = config.raw ? raw : decoded;
            var cookies = document.cookie.split('; ');
            for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
                if (decode(parts.shift()) === key) {
                    var cookie = decode(parts.join('='));
                    return config.json ? JSON.parse(cookie) : cookie;
                }
            }
            return null;
        };
        config.defaults = {};
        $.removeCookie = function (key, options) {
            if ($.cookie(key) !== null) {
                $.cookie(key, null, options);
                return true;
            }
            return false;
        };
    })(jQuery, document);

Answer ā„–1

Have you ever wondered about the function of $.cookie? Is it a plugin specifically designed for jQuery?

Instead of relying on $.cookie, have you considered using document.cookie to set cookies?

If you're curious to learn more, check out this informative link: https://developer.mozilla.org/en-US/docs/DOM/document.cookie

Answer ā„–2

Upon reviewing the documentation on jquery.cookie.js, it appears that the expires parameter signifies the number of days.

if ($.cookie('cookieCreditDash') == null)
{
     $('div').text("no cookie");
     $.cookie("cookieCreditDash", 'test', {expires: 1});
} else{
     var data =  $.cookie("cookieCreditDash");
     $('div').text("with cookie: " + data);        
} 

http://jsfiddle.net/HeGhf/

Answer ā„–3

It appears that you have integrated the jQuery cookie plugin into your project.

In addition to handling cookie expiry concerns, it is important to ensure that your cookie path is properly set to make it accessible across all paths on your domain. To achieve this, you can use the following code snippet:

$.cookie("cookieCreditDash", "Value", { path: '/' });

If you omit the {path: '/'} parameter, the cookie path will be restricted to the current path only.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Status 0 when making an XMLHttpRequest

Hey there, I'm having some trouble with XMLHttpRequest. When I call xmlhttp.send(post), the response I receive shows xmlhttp with state 1 and status 0. I understand that state 1 means the server connection is established, but why is the status showing ...

Is there a way to retrieve the `this.$route` data while using vue-apollo?

Currently working on building a GraphQL query with vue-apollo and graphql-tag. When I manually set the ID, everything runs smoothly. However, I'm aiming to dynamically pass the current route's ID as a variable to Vue Apollo. Successful Implemen ...

Alter the configuration of a JSON object

I'm currently working on modifying the following JSON text. It has the structure as shown below: { "cabecera": { "tipo_cambio": "", "fecha_emision": "", "total": "" }, "detalle": { "940b130369614bd6b687dc5b41623439": { " ...

implementing a collapsible sidebar menu with CSS tables

I am working on a layout with three columns created using css tables to perfectly fit the browser window. The first column serves as a menu and I want it to hide or move to the left when clicked, allowing the middle column to expand into its space. However ...

An issue arising from code in Python, JavaScript, HTML, and CSS

I am currently studying JavaScript and Python and have encountered an issue with my code. I wrote a script that is supposed to create a file and input data into it, but the recv_data function is not functioning correctly - the file is not being created. Ca ...

retrieving identifiers from a separate table for an array of values

As a newcomer to node and noSQL databases, I am facing challenges in grasping the concept of passing an array of IDs and retrieving the corresponding values from another table. I have 'users' and 'products' tables in my database. The st ...

Error message: Unable to access $controller with AngularJS and Karma

As someone who is just starting with testing, I figured it was a good idea to begin testing this project. However, when I execute grunt karma:watch, I encounter an error related to the configuration files. My config file includes: module.exports = functi ...

JavaScript array as a reliable data storage solution

This is my unique jsp code where I am attempting to push certain data into a JavaScript array. <% int proListSize = proList.size(); ProfileDAO proDAO = null; for(int i = 0, j=1; i < proListSize; i++){ proDAO = ( ...

Break down an array-like object with no duplicates

I'm currently learning about working with JavaScript arrays and have a question regarding creating new array objects by splitting an attribute of an existing object. I attempted using methods like .map and .flatMap, but the output I received consiste ...

When performing web scraping with Puppeteer, employing a single selector to target various types of data

As a budding web developer, I have recently delved into coding. My current knowledge is limited to HTML, CSS, JS, and NODE. Currently, I am working on a project involving page scraping and utilizing puppeteer. CHALLENGE - In scenarios like the o ...

Retrieve JSON information from a web-based service in order to create a stunning photo collection

Is there a web-service accessible at ?limit=12&skip=0 I am looking to retrieve the information from this service and store it in a Javascript array. After some research, I believe I should start with the following code snippet: $.ajax({ type: " ...

Having trouble with sslforfree on Node.js Express.js platform

When attempting to set up an HTTPS server using Node.js and Express.js with sslforfree, I encounter an error 403: access denied when trying to access https://localhost. My folder structure looks like this: https://i.sstatic.net/NZqZT.png Below is my serve ...

Pass data back and forth between app.js (node) and javascript (main.js)

I am facing a challenge in sending and retrieving data (username) between app.js and main.js. In my setup, I have a node app.js that calls index.html which then triggers the main.js function called "clicked". Below is the code snippets for each file: app. ...

Template7 produces a bizarre outcome referred to as "each this" whenever JSON is utilized

Currently, I am experimenting with dynamically loading content on Framework7/Template7 pages. Everything works perfectly fine when using static "context" in JSON format. However, when attempting to test it with real-world data from an API, I encounter a st ...

Receiving a sleek black overlay with dynamic text upon hovering over an image

I have been searching extensively for a solution to my issue, but I haven't been able to make it work in my application. What I want is to create a black overlay over an image when it is hovered over, with text that resembles a button appearing on top ...

Activating a trigger in Liferay 7 by loading and executing JavaScript code

Iā€™m looking to implement a new functionality where I load a script and then trigger an event upon its execution. In the past, we used jquery for this purpose but now we are shifting to Alloyui. Currently, I am facing some challenges while trying to achie ...

What steps can I take to maintain persistent authentication in AngularJS?

As a newcomer to angular js, I am currently working on a demo application that features a login screen as the initial view. Upon successful login, other views are loaded along with a navigation bar included using ng-include in the index.html page. The navi ...

How can you make a drop-down menu in React/Bootstrap appear above all other elements?

My current goal is to implement a Bootstrap dropdown menu that appears when a button is clicked. Within my component structure, the code looks like this: <> <div className='1'> <navbar/> <div> < ...

Executing several AJAX functions within a single page of functions

Is there a way to consolidate all database queries onto a single page at the root of each web section? This would help streamline the process and improve efficiency. Currently, I am using a script that allows SQL searches to appear as the user types. Here ...

Is there a way to ensure uniform display of HTML form error messages across various browsers?

Recently, I completed a login form that consists of username and password fields. For the username, I used a text field with the type email, while for the password, I utilized a text field with the type password which requires validation through a regex pa ...