Wednesday 10 July 2019

How do I set and get a cookie with jQuery?

<script type='text/javascript'>
function setCookie(key, value) {
         var expires = new Date();
         expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
         document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}
setCookie('Name','Kishan');

function getCookie(key) {
          var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
          return keyValue ? keyValue[2] : null;
}
getCookie('Name');

// Output: "Kishan"
</script>

No comments:

Post a Comment