...

View Full Version : Store value in cookie



kieran82
12-10-2011, 06:52 PM
I'm new to using cookies. I am using JavaScript for a web project and I'm using cookies and I have 5 links for different movie trailers and I have a trailers.html page with a div with a id="movie" and want to know so that when you click on a Movie trailer link so that it stores a value in a cookie so that you can check on the trailers page which trailer was chosen and display in the div tag

Philip M
12-10-2011, 07:14 PM
Cookies have been covered very many times in this forum. Try using the search feature.


Quizmaster: What "G" is the national dish of Hungary?
Contestant: Goats

tehmarto
12-10-2011, 08:19 PM
Here is something I made up:


<html>
<a href="" id="save_movie" value="1">movie 1</a>
<a href="" id="save_movie" value="2">movie 2</a>
<a href="" id="save_movie" value="3">movie 3</a>
<a href="" id="save_movie" value="4">movie 4</a>
<a href="" id="save_movie" value="5">movie 5</a>
<a href="" id="save_movie" value="6">movie 6</a>
<a href="" id="save_movie" value="7">movie 7</a>
<button onclick="get_movies()">Get movies</button>
</html>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$("a#save_movie").click(function(event){
event.preventDefault();
var read_cookie = readCookie("movies");
if(read_cookie == "null")
document.cookie = "movies="+$(this).attr("value");
else
document.cookie = "movies="+read_cookie+" "+$(this).attr("value");
});
function get_movies()
{
alert("selected id's:"+readCookie("movies"))
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum