是否使用jQuery Cookie插件有效的测试方式来查看是否启用了Cookie? [英] Is using the jQuery Cookie plugin a valid way of testing to see if cookies are enabled?

查看:129
本文介绍了是否使用jQuery Cookie插件有效的测试方式来查看是否启用了Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,我们要求用户在登录网站前启用JavaScript和Cookie。 (JS部分完成并且工作完美。)目前,我们一直设置一个cookie,然后将用户重定向到另一个页面(在PHP中)。这已经工作正常,但现在我们有一群人已经将我们重定向到的页面,当然没有设置的cookie,因此不允许他们登录。

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.

所以我试图找到另一个解决方案来检查cookie,我想使用 jQuery Cookie插件。我想知道是否与所有浏览器兼容(当然,当JS启用时)?

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)?

推荐答案

Thxs Shawn for < =http://stackoverflow.com/questions/2029725/is-using-the-jquery-cookie-plugin-a-valid-way-of-testing-to-see-if-cookies-are-en/2029758# 2029758>您的答案,但不幸的是,因为浏览器并不总是发送引荐来源,它不够可靠,每次都能使用它。因为如果它没有设置,那么你就会陷入循环。

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.

我想到的另一个解决方案是重定向到一个完全独立的页面,它本身检查以查看是否启用了Cookie(通过重定向到自身)。如果启用Cookie,则会重定向回原始页面。如果他们不是,那么它会重定向到一个关于该问题的页面。我认为应该工作,但我不知道。

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.

最后,我尝试了jQuery Cooke插件在IE 6,7和8,Safari 4, Google Chrome 4,Firefox 3.5,Opera 10.2和几个不同的配置,它在所有的工作。下面是我使用的代码:

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
}

这不是完美的,但我认为它会在95%的情况下工作。否则,它会失败,只是不允许他们做任何事情。

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.

这篇关于是否使用jQuery Cookie插件有效的测试方式来查看是否启用了Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆