Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using the jQuery Cookie plugin a valid way of testing to see if cookies are enabled?

I have a site that we require the user to have enabled JavaScript and cookies before they can login to the site. (The JS part is done and works perfectly.) At the moment, we have been been setting a cookie and then redirect the user to another page (in PHP). This has worked fine, but now we have a bunch of people that have bookmarked the page we are redirecting to, which of course doesn't have the cookie set and therefore doesn't allow them to login.

So I'm trying to find another solution to check for the cookie and I'm thinking of using the jQuery Cookie plugin. I'm wondering if it's compatible in all browsers (when JS is enabled of course)?

like image 838
Darryl Hein Avatar asked Jan 08 '10 18:01

Darryl Hein


People also ask

How check cookie is set or not in jQuery?

If this is the case, then you can actually check if a user has enabled cookies or not using the following straightforward jQuery snippet: $(document). ready(function() { var dt = new Date(); dt. setSeconds(dt.

What is jQuery cookie plugin?

A simple, lightweight jQuery plugin for reading, writing and deleting cookies.

How do you check if cookies are enabled JavaScript?

To check whether a setting a cookie is enabled in the browser, you can use the cookieEnabled property in the window. navigator global object in JavaScript. The property will return a Boolean true if cookie enabled and return false if not enabled.


1 Answers

Thxs Shawn for your answer, but unfortunately because browsers don't always send the referrer, it isn't reliable enough to be able to use it every time. Because if it isn't set, then you kind of end up in a loop.

The one other solution that I thought of was to redirect to a completely separate page, which in itself checks to see if cookies are enabled (by redirecting to itself). If cookies are enabled, it would redirect back to the original page. If they are not, then it would redirect to a page about the problem. I think that should work, but I'm not sure.

In the end, I tried the jQuery Cooke plugin in IE 6, 7, and 8, Safari 4, Google Chrome 4, Firefox 3.5, Opera 10.2 and on a few different configurations and it worked in all of them. Here is the code I'm using:

$.cookie('test_cookie', 'cookie_value', { path: '/' });
if ($.cookie('test_cookie') == 'cookie_value') {
    // cookie worked, set/enable appropriate things
}

It's not perfect, but I'm thinking it will work in 95% of cases. Otherwise, it will fail and just not allow them to do anything.

like image 73
Darryl Hein Avatar answered Nov 02 '22 05:11

Darryl Hein