ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
JavaScript Tutorial - Index
JavaScript Tutorial - Introduction to JavaScript
JavaScript Tutorial - Basic JavaScript Concepts
JavaScript Tutorial - Working with Operators and Variables
JavaScript Tutorial - How to work with Loops and Functions?
JavaScript Tutorial - How to work with Forms and Events?
JavaScript Tutorial - How to work with Objects?
JavaScript Tutorial - How to work with Browsers and Cookies?
JavaScript Tutorial - More about Objects
JavaScript Tutorial - How to validate Forms with JavaScript?
JavaScript Tutorial - Advanced Techniques in JavaScript
     

 
Online Personal Trainer Certification
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > JavaScript Tutorial > How to Work with Browser and Cookies?
 

JavaScript Tutorial - How to work with Browser and Cookies?

 
Page 2 of 2
 

Creating Cookies

A cookie is a small piece of data that is stored locally by your browser. It contains some data:

  • Name - containing the actual data, i.e., the name of the cookie.
  • Date - expiry date after which the cookie will no longer be valid
  • Domain and Path - the domain and path of the server, where the cookie should be sent to.

As you know that, cookies in JavaScript are accessed using the cookie property of the document object. In other words, a cookie can be created like this:

Syntax
document.cookie = "name=value; expires=date; path=path; domain= domain; secure";

To set a cookie, the property document.cookie is set to a string, containing all the properties of the cookie, that you want to create. The syntax of  set-cookie is used to transfer significant information between client and server. While assigning a new cookie to the client, in an HTTP response, the following syntax is followed :

 

Syntax
Set-Cookie : NAME = value [;EXPIRES = date] [;PATH = path] [;DOMAIN = domain] [;SECURE]

The NAME = value is a compulsory field that must be appended in the Set-Cookie field whereas all the other entries are optional. The server can send more than one Set-Cookie fields.

         Name                                                     Description
NAME = value It specifies the name of the cookie, by which you can reference it later. It contains the actual data that the cookie is going to store. The use of semi-colon, comma, or whitespace is illegal in the NAME=value field.
EXPIRES = date Every cookie persists only for a specific duration. This field specifies the expiry date of the cookie, after which it is scraped, i.e., after this date the cookie will no longer be stored by the client or sent to the server. The syntax for specifying expiry date is :
Weekday, DD-MON-YY  HH : MM : SS  GMT, Weekday is the day of the week, DD is the day of the month, Mon is the month, YY is the year, HH is hours, MM is minutes and SS is seconds. The GMT is the Greenwich Mean Time. By default, the value of 'Expires' is set to the end of current Navigator session.
PATH = path The path attribute specifies the portion of the URL for which the cookie is valid, i.e., it specifies the directory where the cookie is active. Usually the path is set to '/' which means the cookie is valid during the entire program.
DOMAIN= domain This field specifies the domain portion of the URLs for  which the cookie is valid. For example, if the cookie is served by  www.expert.com, then it also relates to expert.com and www.expert.com/solutions.htm.
SECURE It specifies that the cookie should be transmitted over a secure link only, i.e., if this field is specified, a cookie is sent to HTTP servers using SSL (Secure Sockets Layer) protocol known as HTTPS servers. If this attribute is not specified, the cookie will be sent over any channel, involving an unsecured one also.

Retrieving the Cookies

The document.cookie property is again used to retrieve all the previously set cookies applicable for the current document.

Syntax
var c = document.cookie;

This property will return a string consists of name-value pairs, for all the cookies applicable for the current document. To read the value of a cookie, pass the individual cookie name as a parameter in Get Cookie field.

Syntax
function getCookie (Cookie Name)

Cookie Name is the name of the cookie as you stored it earlier. The function getCookie( ) returns a string containing the value of the particular cookie or null if the cookie does not exist.

Deleting a Cookie

 The function delCookie( ) is used to remove or delete a cookie. It will delete a specified cookie from the browser by setting its expiration date in the past or set day to 0, in Set Cookie. To use this function simply pass the cookie name to the delCookie( ).

 
 
Summary
 

In this chapter you have learnt:

  • What's the need of browser detection.
  • Accessing properties of navigator object.
  • Purpose of cookies.
  • How to create , retrieve and delete an unwanted cookie.

 

 
Review Questions
 

Fill in the Blanks

  1. The browser information can be revealed using --------- object.
  2. Navigator object's --------- property reveals that whether a cookie has been enabled or not.
  3. -------- helps storing information locally in the browser.
  4. The ------- field is compulsory, while setting a cookie.

Solutions

  1. Navigator
  2. cookieEnabled
  3. Cookie
  4. Name

True or False

  1. Navigator object reveals information about the browser version and browser name only.
  2. Cookie is used to store information on client side only.
  3. Cookies can persist forever.

Solutions

    1. False
    2. True
    3. False

 

Exercise
  1. Create a cookie, which remembers the name of the visitor, when he re-returns to the page.
Solution
  1. View Code              View Runtime

 

 
What's Next

The next chapter will acquaint you with the concepts of JavaScript's window object and JavaScript menu. The chapter will further elucidate about properties and methods of window object and pop-up windows.
                    Hop over to the next chapter to get the close-up of window object and JavaScript menu.

 

       

 

 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2015. All Rights Reserved.