Checking for Cookie Support


document.cookie="CookieTestTemp=success" document.cookie="CookieTestPers=success; expires=Tue, 25-Dec-2012 12:34:56 GMT" 

The only way to actually find out whether a cookie has been successfully set is by checking for the cookie upon the next request to the server. However, with JavaScript, this can be done instantly within one page. The following code first sets two cookies (a temporary one and a persistent one) and then checks whether these two actions were successful. Also, the test cookies are deleted againbut of course only if setting them has been successful.

Checking the Browser's Cookie Support (checkcookie.html)

<script language="JavaScript"   type="text/javascript"> document.cookie="CookieTestTemp=success" document.cookie="CookieTestPers=success; expires=Tue, 25-Dec-2012 12:34:56 GMT" if (document.cookie.indexOf(   "CookieTestTemp=success") != -1) {   window.alert("Temporary cookies are supported!")   document.cookie="CookieTestTemp=JavaScript; expires=Thu, 25-Dec-1980 12:34:56 GMT" } else {   window.alert(     "Temporary cookies are not supported!") } if (document.cookie.indexOf(   "CookieTestPers=success") != -1) {   window.alert("Persistent cookies are supported!")   document.cookie="CookieTestPers=JavaScript; expires=Thu, 25-Dec-1980 12:34:56 GMT" } else {   window.alert(     "Persistent cookies are not supported!") } </script> 




JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net