
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to set cookies to expire in 1 hour in JavaScript?
You can extend the life of a cookie beyond the current browser session by setting an expiration date and saving the expiry date within the cookie. This can be done by setting the ‘expires’ attribute to a date and time.
Example
You can try to run the following example to set cookies to expire in 1 hour −
<html> <head> <script> <!-- function WriteCookie() { var now = new Date(); now.setTime(now.getTime() + 1 * 3600 * 1000); cookievalue = escape(document.myform.customer.value) + ";" document.cookie="name=" + cookievalue; document.cookie = "expires=" + now.toUTCString() + ";" document.write ("Setting Cookies : " + "name=" + cookievalue ); } //--> </script> </head> <body> <form name="myform" action=""> Enter name: <input type="text" name="customer"/> <input type="button" value="Set Cookie" onclick="WriteCookie()"/> </form> </body> </html>
- Related Articles
- How to set cookies to expire in 30 minutes in JavaScript?
- How to set named cookies in JavaScript?
- How to set multiple cookies in JavaScript?
- How to set cookies expiry date in JavaScript?
- How to set cookies in ReactJS?
- How to use JavaScript to set cookies for homepage only?
- How to use JavaScript to set cookies for multiple pages?
- How to create cookies in JavaScript?
- How to delete cookies in JavaScript?
- How to use JavaScript to set cookies for a specific page only?
- How to store large data in JavaScript cookies?
- How to access cookies using document object in JavaScript?
- How to clear all cookies with JavaScript?
- How do you set cookies in the JSP?
- How to get the cookies associated with a document in JavaScript?

Advertisements