Join us on Facebook!
21
Jan

Determining JavaScript cookie support in client's browser

by admin 6035 views 0 Comments and 0 Rections

If your script relies on JavaScript cookies to persist and store information for retrieval later, it's good practice to always make sure that the user's browser supports cookies first. This includes whether the browser has cookies enabled (an option in most browsers). Depending on your situation you can then either remind the user to enable cookies, or create code to handle these cases silently.

So, to answer this common question, use the following technique to detect whether the client's browser has cookies enabled: 

The above script is actually two techniques rolled into one:

  • Firstly, we use the official property for the task, navigator.cookieEnabled, to help determine whether the browser has cookies enabled. However, we immediately run into a limitation- this property is supported only in IE4+ and NS6+. So, in both cases where the property returns false or does not exist (in other browsers), we set the variable cookieEnabled to false instead for continued investigation.
  • Moving on, it's now time to probe all other browsers for cookie availability. The age-old "give it a try" technique works nicely. By setting a dummy value to document.cookie and immediately probing it to see if the value is retained, we can tell if cookies is enabled on the client's end.

The variable cookieEnabled now contains a Boolean value indicating the client's cookie support. Nice and crunchy!

More Posts in "Tutorials"

Comments

blog comments powered by Disqus

Feed Subscription

Get daily inboxed with latest news, reviews and tutorials. Signup now it's free :)



Planetmaks on Twitter

Follow PlanetMaks on Facebook

Recent Comments

Powered by Disqus

Tags

32Bit 64Bit 7 Admin Tool Admin Tools Admin-Tool Admin-Tools AdminTool Adobe Adware All-in-One Anti-Spyware Anti-Virus Apple Backup Tools Browser Browsers Changing Default Directory Codec Data Recovery Download Manager Downloads Drive Utilities Driver Drivers Firefox Flash Freeware Google Graphics Internet Tool Internet-Tool Messaging Microsoft Microsoft Tool Micsellonous Monitoring Multimedia Networking News NVidia Office Tool Office-Tool PC Suite Press Release Registry Samsung Security Shareware Software Softwares Tutorials Vista Windows Windows 7 Windows 8