Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Store value in cookie

    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is something I made up:
    Code:
    <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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •