What is the best method for determining the cookie expiration time in AngularJS 1.3?

Currently in my application, I am utilizing AngularJS 1.3. I encountered a challenge while using $cookies to store data where I needed to implement a 1-minute expiration time for the cookie. However, the $cookies service in AngularJS 1.3 does not provide a built-in method to set an expiration time.

I attempted the following code snippet to address this issue:
var x = new Date();
x.setMinutes(x.getMinutes() + 1);

$cookies['showStartUpScreen'] = value + ';expires=' + x.toGMTString();

Answer №1

In version 1.4 of Angular, the ability to specify an expiration time for cookies was introduced, alongside a complete overhaul of how Angular interacts with cookies in general. Prior to this update, in version 1.3, it was possible to use plain JavaScript to set a cookie with an expiration time, as Angular monitored for any changes to cookies.

var date = new Date();
var expireTime = date.getTime() + 5000; // expires after 5 seconds
date.setTime(expireTime);
console.log(date);
document.cookie = 'myFavorite=ok;expires=' + date.toGMTString() + ';path=/';

// Retrieve the cookie using both vanilla JS and Angular
console.log(document.cookie);
var theCookie = $cookies.myFavorite;
console.log(theCookie);

To demonstrate the expiration functionality, I have created a proof of concept on CodePen. Simply click a button after 5 seconds to see that the cookie indeed expires as expected.

For further reading, here are some helpful resources:

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

export default select an option

Forgive me if my question comes off as naive, but I'm still learning the ropes. :-) I stumbled upon something perplexing in this GitHub file. I am aware that we can export or import default functions, but in this instance, the author has used: expo ...

Guide to transferring existing Mongoose Schema information to updated Mongoose Schema Data

In the schema I was using previously with Mongoose, it looked like this: social:{ instagram: String, facebook: String, whatsapp: String, } Now, my updated Mongoose Schema is structured differently: social:{ instagram: { dat ...

Getting services in a directive in AngularJS by function name: How to do it?!

I am looking for a way to incorporate a reusable pattern in my directive, such as displaying a modal with a list of data. The modal will have a method to retrieve data, which is defined in a service. The service is dynamically generated based on the parame ...

Is there a way to connect two distinct JSON objects using key values in AngularJS?

I just started learning angularjs and I have a json data with two objects - categories and sub_categories. { "status": true, "data": { "categories": { "1": "Fasteners", "2": "Paints", "3": "Abrasives" ...

Unable to access the "target" property while transferring a value from child to parent component

Within my project, the student component is considered a child component of the main app component. Inside the template of the student component, there is an input element defined like so: <input type='text' #inputbox (keyup)='onkeyUp(i ...

Encountering a situation where attempting to retrieve JSON data results in receiving an undefined

After fetching JSON data from the API , I successfully printed out text to the console with the correct data. However, when attempting to access any of its properties, they are returning as undefined. var url = "http://www.omdbapi.com/?t=batman&y=& ...

Modifying Characteristics of Elements Added Dynamically

How do I change the type attribute for dynamically added buttons? In the code below, the label names are changing perfectly, but when I try to change button types, it applies to all dynamically added buttons. My requirement is to change every button type t ...

Retrieve information from a JSON file and dynamically showcase it within a REACT component

Hey there! I'm a newbie in the world of programming and currently working on creating a carousel using Bootstrap and React. My aim is to make the Carousel images dynamic by fetching data from a local .json file. However, my current implementation seem ...

Get rid of unsafe-eval in the CSP header

I am facing an issue with my old JavaScript code as it is using unsafe-eval. The client has requested to remove unsafe-eval, but the code relies on the eval method in all JavaScript libraries. Removing unsafe-eval breaks the functionality of the code. How ...

How can you use the :not selector in CSS to target specific elements within a group?

Consider the following set of inputs: <div id="group"> <input id="uno" class="red"></input><br/> <input id="dos" class="red"></input><br/> <input id="tres" class="blue"></input><br/ ...

Karma is unable to locate the module within the specified relative path

I'm facing a challenge with Karma not being able to load a specific file. As a novice in Karma, I dedicated the entire day to researching and checking documentation for similar issues with no luck. Upon initiating the karma process, it encounters an ...

Understanding the distinction between deleting and nullifying in JavaScript

var obj = {type: "beetle", price : "xxx", popular: "yes" ,.... }; If I want to remove all the properties from obj, what is the correct way to do it? Should I use delete obj.type; delete obj.price; delete obj.popular ... and so on. Or should I set ob ...

The progress event of the XMLHttpRequest updates quickly, outpacing the speed of the upload itself

I've been working on setting up an upload form and using xhr to return the upload status to the user. Everything seems to be in place, but I'm facing a strange issue where the callbacks are happening too quickly and showing a higher percentage th ...

What steps do I need to take to implement AJAX form authentication?

I have set up a login screen that validates username and password credentials through a php script. When a user enters their information and submits the form, the authenticate.php file executes an LDAP bind. If the credentials are correct, the user is redi ...

Move the dist folder to the libs directory using webpack

I am interested in achieving the following task: After successfully using gulp for copying libraries, I added the below code to my tasks: gulp.task('copy:libs', function() { return gulp .src(npmdist(), { base: paths.base.node.dir }) . ...

Implementing event listeners with Angular UI-Router

Currently, I am working with Angular 1.4.5 and Angular Ui Router. I am facing an issue where I am trying to utilize addEventListener to execute a function when a text field is blurred. The challenge I am encountering is that during the load state, the elem ...

Having trouble with opening and closing popup windows in JavaScript while using Android frames?

To display additional information for the user, I create a new tab using the following code: window.open("/Home/Agreement", "_blank"); Within the Agreement View, there is a button with JavaScript that allows the user to close the Popup and return to the m ...

Utilizing camera perspective for coordinate system in Three.js

This question may seem basic, but I'm having trouble grasping it. I'm working on a scene using Three.js and the camera setup is as follows: var camera = new THREE.PerspectiveCamera( 85, window.innerWidth/window.innerHeight, 0.1, 1000 ); camera. ...

Angular version 1.63 with UI-Router version 1.0 brings a new feature: local transition hooks that

I am in search of a transition hook event specific to my application's local environment. For instance, Whenever controller A is instantiated, I aim to have all transitions from state foo to state foo.bar be recorded. While going through the Transiti ...

Failure of event watcher on dynamically updated content

Any help would be greatly appreciated! :) I am currently using JavaScript to dynamically add fields to a document, but I have noticed that the event listener only works on predefined fields. For instance, in the code snippet below, the 'lozfield&apo ...