Yes, jQuery can read and write cookies. Although jQuery does not have built-in functions for handling cookies, we can simplify cookie operations by introducing additional plugins, such as the jquery.cookie plugin.
For example, using the jquery.cookie plugin, we can handle cookies as follows:
Writing a cookie:
javascript$.cookie('user', 'John Doe', { expires: 7, path: '/' });
This line creates a cookie named 'user' with the value 'John Doe' and an expiration of 7 days. The path: '/' ensures the cookie is accessible across the entire website.
Reading a cookie:
javascriptvar user = $.cookie('user');
This line reads the value of the cookie named 'user'.
Deleting a cookie:
javascript$.removeCookie('user', { path: '/' });
This line deletes the cookie named 'user'.
Using such plugins makes it easier to handle cookies in jQuery projects without manually writing JavaScript for reading and writing cookies. This is very helpful for maintaining code simplicity and improving development efficiency.