Scripts — Javascript Cookies Enabled Test
How do you determine if Cookies are enabled with Javascript? It's very simple. First, get the Javascript Cookie script if you haven't downloaded it already.
Your Computer
Javascript
The main idea here is to run through the script and Set Cookie Get Cookie functions one after another. You can do this in Javascript because javascript has access to the document.cookie data which server side scripting languages like php do not.
Directions on how to check if Cookies are Enabled with Javascript.
You will be placing the following code into your webpage, it can run anywhere. For our purposes we are setting the cookie with name 'test', value 'none', set to expire in no days, which will set the expires feature to ''. '' means that the cookie is what is known as a session cookie, it will expire as soon as the browser session ends. If you see any bugs or other glitches with the code, please post them on the web scripting forum.
First you would have placed your cookie script into a library file which is called in the head of your html document like this (please note that this is just an example, if you don't have a devepment server setup, you'll want to replace the '/' absolute to root path with a relative path, like this '../js/javascript_cookies.js'.
<script type="text/javascript" src="/js/javascript_cookies.js"></script>
Then you place the following code anywhere on the page AFTER the library file has been loaded. If you are outputting something, you'll want it after the <body> tag of course. If you are just loading a variable, you can put this code anywhere, including in the library file, then just check the value of the cookie set variable.
<script type="text/javascript"> // remember, these are the possible parameters for Set_Cookie: // name, value, expires, path, domain, secure Set_Cookie( 'test', 'none', '', '/', '', '' ); // if Get_Cookie succeeds, cookies are enabled, since //the cookie was successfully created. if ( Get_Cookie( 'test' ) ) { document.write( 'cookies are currently enabled.' ); /* this is an example of a set cookie variable, if you want to use this on the page or on another script instead of writing to the page you would just check that value for true or false and then do what you need to do. */ cookie_set = true; // and these are the parameters for Delete_Cookie: // name, path, domain // make sure you use the same parameters in Set and Delete Cookie. Delete_Cookie('test', '/', ''); } // if the Get_Cookie test fails, cookies //are not enabled for this session. else { document.write( 'cookies are not currently enabled.' ); cookie_set = false; } </script>
Obviously Javascript must be enabled for this test, and the Cookies, to work. There is as far as we know no way to check this server side without using a page reload, which is not a very good idea in general.