Thursday, November 28, 2024

Deleting a Cookie in JavaScript by name.

 

Set-Cookie

function set_cookie(name, value) {

     document.cookie = name +'='+ value +'; Path=/;';

}

Delete-Cookie

function delete_cookie(name) {

      document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';

}

Get-Cookie

function get_cookie(name);

Example:- Set, delete, and Get a specific cookie with the name userlogset.

1. set_cookie('userlogset', '123456');

2. let userlog = getCookie("userlogsetcok");

alert(
userlog);

3. delete_cookie('usrlogset');


Drupal way:-

(function ($, Drupal, drupalSettings) {

  'use strict';

  Drupal.behaviors.globalInfoData = {

    attach: function (context, settings) {

      var uid = settings.user.uid;

      if(!uid) {

        let getuserlog = getCookie("userlogset");

        if(getuserlog) {

          Drupal.behaviors.globalInfoData.delete_cookie('userlogset');

        }

      }

    }

  };

})(jQuery, Drupal, drupalSettings);

Function call:-

Drupal.behaviors.globalInfoData.delete_cookie =function (name) {

    document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';

 }

No comments:

Post a Comment

If you have any problem please let me know.