google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Browser Cookie with JavaScript and jQuery This short JavaScript article tutorial guides you how to work with browser cookie, cookie in JavaScript by using the JavaScript framework jQuery, through some basic missions: set JavaScript cookie and store data values into cookie; JavaScript read cookie, get stored values and use them for your web applications. A very helpful JavaScript tutorial for JavaScript beginners.


Free iPage Web Hosting for First Year NOW



If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?

Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Try iPage for FREE First Year NOW

Overview on using cookies via jQuery or JavaScript.

Today i want to go over a few techniques to easily use cookies via jQuery (much easier) or pure javascript.
What is a cookie?
A cookie is a text string that is included with Hypertext Transfer Protocol (HTTP) requests and responses. Cookies are used to maintain state information as you [...]

Overview on using cookies via jQuery or JavaScript.

Today i want to go over a few techniques to easily use cookies via jQuery (much easier) or pure javascript.

What is a cookie?

A cookie is a text string that is included with Hypertext Transfer Protocol (HTTP) requests and responses. Cookies are used to maintain state information as you navigate different pages on a Web site or return to the Web site at a later time. This article provides information about cookies.

What do i do with cookies

Cookies today can be seen used most often for storing user logged in information. That means that with cookies we can allow for our members to set a certain amount of time for their account to stay logged in. Also you use cookies to store very basic temporary information that the website collected from you at one point of time.

Cookies should NOT be used to store information that needs to be accessible by the user or by your website from a variety of places. By places i am referring to different machines and browsers that store the cookies locally.

How do i actually use them

Cookies are accessible by all web programming languages. That means, javascript, python, php, and many more. First i want to go over using cookies with jQuery since it is the easiest way to do this.

jQuery has a plugin you use with the jQuery library itself and you can really easily store and access your cookies. So lets look at the code of storing and displaying a cookie.

  1. <html>  
  2. <head>  
  3. <script src="http://code.jquery.com/jquery-latest.js"></script>  
  4. <script src="jquery.cookie.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     // set cookie  
  8.     $.cookie('my_cookie_name', 'value inside of it');  
  9.   
  10.     // get cookie  
  11.     alert($.cookie('my_cookie_name'));  
  12.   
  13.     // delete cookie  
  14.     $.cookie('my_cookie_name', null);  
  15. });  
  16. </script>  
  17. </head>  
  18. <body>  
  19. <a class="setme">Set Me</a>  
  20. <a class="tellme">Tell Me</a>  
  21. <a class="delete">DeleteM</a>  
  22. </body>  
  23. </html>  

Now let's go over the important parts of the code. First of course we embed jQuery itself, then we add the jquery cookie plugin and then we start our beautiful jQuery code.

The set cookie is pretty straight forward, one line of code that will set the cookie "my_cookie_name" with the value "value inside of it". We do that by passing in two parameters into our $.cookie() plugin. You do need the quotes around the name and the value of the cookie, feel free to use single or double quotes as your preference.

The get cookie code is even more straight forward we just pass in the cookie name in out jquery cookie plugin and it has the value we stored in there.

And finally deleting the cookie is almost the exact same as storing the cookie, only difference is we store null inside of it.

Now let's go more advanced

Now that we have set a cookie, added a value, and deleted the stored value it's time to do things like storing the cookie for a certain period of time, or to a certain date!

  1. // cookie expires in 10 days  
  2. $.cookie('cookie_name''our value', { path: '/', expires: 10 });  
  3.   
  4. // cookie expires in a set JavaScript Date  
  5. var date = new Date();  
  6. $.cookie('cookie_name''our value', { path: '/', expires: date });  

The first code sets a cookie to expire in 10 days. That's it... it's as simple as that. Pass in a third parameter that has the path to where you want to store the cookie in the browser directory, leaving this to / is your best choice. And then you set the expiration days.

The second code we first set a variable date to store the current date and then it will set the cookie to expire to the certain JavaScript date. Putting a real date in there will not work unless you stored a javascript date in a variable called date. Read more about JavaScript dates here.

How to do this with JavaScript

Here is the code to write a cookie with plain JavaScript

  1. function setCookie(key, value) {  
  2.    var expires = new Date();  
  3.    expires.setTime(expires.getTime() + 31536000000); //1 year  
  4.    document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();  
  5.    }  
  6.   
  7. function getCookie(key) {  
  8.    var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');  
  9.    return keyValue ? keyValue[2] : null;  
  10.    }  
  11.   
  12. setCookie('test''5');  
  13. alert(getCookie('test'));  

We set up two functions setCookie and getCookie. We need to pass in 2 variables into the setCookie the key and the value pair. Currenty the cookie is set to expire in a year but of course you can change that, i have the link on javscript dates above.

I hope i have shed some light on the world of cookies!

AIVideo-App.com
Generate your business videos by AI with voice or just text

chatGPTaz.com
Talk to ChatGPT by your mother language

AppAIVideo
Your first FREE AI Video App

Deepfake Video
Deepfake AI Video Maker

Deepfake
Deepfake AI Video Maker

AI Deep Fake
Deepfake AI Video Maker

AIvidio
AI Video Mobile Solutions

AIvideos
AI Video Platform & Solutions

AIvedio
AI Video App Maker

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program

Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing

Dall-E-OpenAI.com
Generate creative images automatically with AI

chatGPT4.win
Talk to ChatGPT by your mother language

First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language

Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

iPhoneKer.com
Save up to 630$ when buy new iPhone 16

Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web