开发者

basic cookie question

开发者 https://www.devze.com 2023-03-02 20:14 出处:网络
for a certain project I\'d like to have an alert/notification box appear upon a user\'s initial visit to a page, and then assuming the user hit开发者_运维百科s the \"x\" to close the notification, it

for a certain project I'd like to have an alert/notification box appear upon a user's initial visit to a page, and then assuming the user hit开发者_运维百科s the "x" to close the notification, it will then cease to appear on subsequent visits-- I'd imagine this requires the setting of a cookie (via PHP), and perhaps the containing alert div responding to the presence or absence of the cookie via jQuery? any basic direction here in how to get started is much appreciated...


You can do this using browser-side cookies: look at the jquery cookie plugin http://plugins.jquery.com/project/Cookie.

EDIT: here's how I'm using it.

Save helpful notices preserves my helpfulnotices object, so I don't show any one helpful notice more than it should be shown.

if(!helpfulnotices.multiupload){
            helpfulnotices.multiupload = true;
            top.flicsy.notify_user(flicsy_lang.message_upload_multiple, "", true);
            self.save_helpful_notices();
        }

Here's the code to save the structure using the jquery JSON cookie extension:

save_helpful_notices: function()
    {
        var date = new Date();
        date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));  // expire in a week or thereabouts
        jQuery.JSONCookie('helpfulnotices', helpfulnotices, {path: '/', expires: date });
    },

and on page load I re-hydrate it:

helpfulnotices = jQuery.JSONCookie('helpfulnotices');


You should be able to set this with a cookie. However, it will not guarantee that the user will not see it next time. It also won't guarantee that new users will see it (if they are on a shared computer).

These things are always best to do in conjunction with a "user login system" so you can track each user individually. Then you can store the setting in the database and recall the setting for each user on subsequent visits.


You can save the information in a cookie, but that will imply that you will send this cookie back and forth all the time from your client to your server.

If you want to prevent this, you can use the new HTML5 variable called localStorage

http://www.bing.com/search?q=html5+localStorage&src=ie9tr

So basically, you can use localStorage if it exists on the browser, if it doesn't fallback with cookies. That would be the optimal solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消