简体   繁体   中英

Managing User Preferences on Future Visits

I'm adding a notification to my website about a new feature that has been implemented. The notification is near the top of the website, in an eye-catching place to get people's attention. Using jQuery, I wrote a small function that hides the notification when the user chooses to. If the user chooses to hide the notification, I want it to be hidden permanently for the remainder of the visit and on future visits to the website.

What would be the best way to make sure that when they return to the website later on, the notification is hidden? I thought about sending an AJAX request that creates a cookie when the user initially hides it, then checking for the cookie when the page loads, but I'm not sure if that's the best way to do it. I don't want to hide it based on IP address because I'm afraid that two users on the same network but different computers might access the website, and one will miss it, but maybe I'm being too paranoid. Any ideas on the best way to do this?

为什么不只使用javascript创建/读取cookie?

I would suggest using javascript to set/read the cookie as well. Since you're already using jQuery I would suggest the jquery.cookie plugin . The only reason for doing an AJAX request to save the user's preference is if your user is logged into the site and you want to store their preference in a database or some other persistent storage so that whenever they are logged in they don't see the message about your new feature.

If the server does not need to be aware of the value of the cookie I would suggest you use localStorage instead.

localStorage exists on the client and does not get sent with the $_COOKIES header to the server.

More can be read about localStorage here

And even more can be read as per why you would want localStorage over cookies for client side data here Local Storage vs Cookies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM