I have a website with movie quotes. The visitor can like a movie quote which adds 1 to it's ranking in a database. That code is working. I'm trying to write code that prevents a user from liking a quote multiple times but that code isn't working. You can see the site at this link Here is what I have so far. Can anyone tell me what I'm doing wrong?
Problem with setting cookies to prevent multiple likes
2
I have a website with movie quotes. The visitor can like a movie quote which adds 1 to it's ranking in a database. That code is working. I'm trying to write code that prevents a user from liking a quote multiple times but that code isn't working. You can see the site at this link Here is what I have so far. Can anyone tell me what I'm doing wrong?
My html is:
Code:
function setLike(id) {
document.cookie=id+'=y';
}
function hasLiked(id) {
var cookies=document.cookie.split(';');
for (var i=0;i<cookies.length;i++) {
var cookie=cookies[i].split('=');
if (cookie[0]==id) return true;
}
return false;
}
var button=document.getElementByClassName('like');
button.onclick=function SetLikeCookie() {
var id=this.getAttribute('id');
if (!hasLiked(id)) {
alert('Your like has been recorded');
setLike(id);
} else {
alert('You cant like a quote twice');
}
} My html is:
Code:
<div><p>"Frankly, my dear, I don't give a damn."</p> <p>Rhett Butler | Clark Gable | Added 9/28/2015</p> </div> <div class="movie-like-image"> <img onclick="increment(this); SetLikeCookie;" id="1" class="like" src="../images/like.jpg"> </div>
- joeelmore
- [1] reply
- AboutTown
Next Topics on Trending Feed
-
2