I need JavaScript code that will detect if a user has cookies disabled in their browser. If they do, they will be redirected to another page. If they have their cookies enabled it just goes straight through as usual.
Asked
Active
Viewed 576 times
-2
-
I need many things too... – slash197 Jun 14 '13 at 05:59
-
I'd suggest a simple [>Google search<](https://www.google.com/search?q=javascript+to+detect+cookies+enabled&rlz=1C1TSNP_enUS471US471&oq=javascript+to+detect+cookies&aqs=chrome.1.57j0l3j62.8473j0&sourceid=chrome&ie=UTF-8) first. Did you even look? – jfriend00 Jun 14 '13 at 06:00
-
Similar thread, http://stackoverflow.com/questions/531393/how-to-detect-if-cookies-are-disabled-is-it-possible – user1390282 Jun 14 '13 at 06:07
-
this is not a question but a request. you need to include what you have done so far when posting a question here. – artsylar Jun 14 '13 at 06:13
1 Answers
1
You can insert a test cookie in browser and call back that cookie again.
Use this library. It contan simple function to identify cookies enabled, create/read or erase cookies.
<script type="text/javascript">
/* function to create cookie
@param name of the cookie
@param value of the cookie
@param validity of the cookie
*/
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name, "", -1);
}
/*
This function will create a new cookie and reading the same cookie.
*/
function areCookiesEnabled() {
var r = false;
createCookie("testing", "Hello", 1); //creating new cookie
if (readCookie("testing") != null) { //reading previously created cookie
r = true;
eraseCookie("testing");
}
return r; //true if cookie enabled.
}
</script>
and your code must be.
<script>
if(!areCookiesEnabled())
{
//redirect to page
}
</script>

Fasil kk
- 2,147
- 17
- 26
-
-
-
This works but it still hits the page for a split second before redirection. Is there anyway to make it instant as i am trying to protect advertising impressions form hit bots that don't have cookies enabled – Bruce Goldberg Jun 14 '13 at 06:50
-