|
Partners
|
Determining JavaScript cookie support in client's browserIf 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: <script type="text/javascript">
var cookieEnabled=(navigator.cookieEnabled)? true : false
//if not IE4+ nor NS6+
if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){
document.cookie="testcookie"
cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
}
//if (cookieEnabled) //if cookies are enabled on client's browser
//do whatever
</script>
The above script is actually two techniques rolled into one:
The variable cookieEnabled now contains a Boolean value indicating the client's cookie support. Nice and crunchy! |