Need a javascript function to read and set a cookie. A single cookie, for this example called "cart", to which items can be added and removed.
with onclick for item 101, cookie.cart=101
with onclick for item 102, cookie.cart=101-102
with onclick for item 105, [login to view URL] =101-102-105
and so on.
Now that cookie cart=101-102-105, a div with id="count" needs to display (3) - count of items in cart.
A click on item 102 again would change cookie to cookie.cart=101-105, count=(2) (remove item 102 from cookie data). The script would need to detect that 102 is already in cookie and in place of adding it, remove it. And of course re-save the cookie with the new data.
It would need to be as elegant and efficient as possible, taking into account that a new cookie may need to be set at start when one does not exist, and so on. Also, I should be able to limit the amount of items, say 50, that can be stored to a cookie, with a custom message when limit reached.
Here is some basic html of the page just for illustration purposes where an onclick event is used setCookie(name,value,days):
<!DOCTYPE HTML>
<html>
<head>
<p><div onclick="setCookie('cart','101','1','\')" style="cursor:pointer;">Select 101</div><div onclick="setCookie('sCart','101','1')" style="cursor:pointer; dislay:none;">Remove 101</div></p>
<p><div onclick="setCookie('cart','102','1','\')" style="cursor:pointer;">Select 102</div><div onclick="setCookie('sCart','102','1')" style="cursor:pointer; dislay:none;">Remove 102</div></p>
<p><div onclick="setCookie('cart','103','1','\')" style="cursor:pointer;">Select 103</div><div onclick="setCookie('sCart','103','1')" style="cursor:pointer; dislay:none;">Remove 103</div></p>
<p><div onclick="setCookie('cart','104','1','\')" style="cursor:pointer;">Select 104</div><div onclick="setCookie('sCart','104','1')" style="cursor:pointer; dislay:none;">Remove 104</div></p>
<p><div onclick="setCookie('cart','105','1','\')" style="cursor:pointer;">Select 105</div><div onclick="setCookie('sCart','105','1')" style="cursor:pointer; dislay:none;">Remove 105</div></p>
<div id="itemcount">0<div>
</body>
</html>
So any time a user clicks any of the links, the javascript will check if that item number is in cookie data. If it is, then remove it from the data string. If it is not, add it to the cookie data string.
I have found something similar to what i am after at this link: [login to view URL], look at very last answer on page. However, this is not all of what i need, and i could not get it to work.
Hello sir, I checked requirement and i think this is you need:
+ need a javascript function to read and set a cookie
Why would i benefit you?
- I am confident about this project, I can start right now.
- I have worked on this field for 6+ years.
- I am good at working under pressure and with deadlines
- I can work on my own, but I can adapt to be a part of the team if required
- I'm a full-time freelancer and I am available to work long hours and on projects that requires working in different time zones
- All my previous projects was feedback 5 stars so my service is best choice
Hoping that you will review my cover letter and feedback, I am looking forward to hearing from you.
Kind Regards
Hi there,
Thank you for sharing your requirement. Hope you are doing good and having a nice day.
I'm confident of quality codes and smart work for this task.
You can check my rich experience and good reviews for previous similar work on my profile.
If you award me this project, we will be glad with good results for each other.
Please contact me to discuss your project in detail.
Thanks. Best Regards.
Hey There, Web Developer here.
I can create a function to modify your cookie data in just 1 day at a flat price of 50/-
function will allow you to add and delete product data from cookie.
I am Microsoft certified in Javascript and Computer Networking.
I am SQL, PHP and Python certified from University of Michigan.
You can check all my certificates.
Looking for your response.
Thankyou
I'm good at javascript, with an extensive experience. I think I can give you better solution for the same, as the cookie is not a right solution in your case,as the cookie comes with a limitation of 5mb data. I can give you right solution for this. which will be helpful to you.