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 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Set a cookie for where I left off in XML list?

    So I have a list of values in an xml document.

    1. Tom
    2. Ned
    3. Steve
    4. Bob
    5. Rob

    If I leave/refresh the page, I start back at the 1st entry (Tom). I would like to return the viewer to where the viewer left off.

    Example, if user exists at Steve, I would like to resume him when he returns at Bob.

    Is this possible using Javascript/XML?

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,248
    Thanks
    12
    Thanked 340 Times in 336 Posts
    that depends on how you loop through the list. if for example you use a for loop, the you can save the starting index in a cookie (and read that value in before starting to loop)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Oct 2010
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So the cookie is being set correctly, but when I refresh the page, the cookie is reset again to 0... I think it has something to do with this part right here..

    cookie = document.cookie.indexOf("name");
    i = cookie;

    i is where the list starts from, but it's not working!!!!

    Thanks for your help.

    Code:
    <script type="text/javascript">
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","cd_catalog.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 
    
    x=xmlDoc.getElementsByTagName("CD");
    cookie = document.cookie.indexOf("name");
    i = cookie;
    
    function displayCD()
    {
    title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
    
    txt=title;
    document.getElementById("test").innerHtml=txt;
    document.cookie = "name=" + i + "; expires=Sat, 14 Jul 2012 05:28:21 UTC; path=/";
    }
    
    function next()
    {
    if (i<x.length-1)
      {
      i++;
      displayCD();
      }
    }
    
    function previous()
    {
    if (i>0)
      {
      i--;
      displayCD();
      }
    }
    </script>

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,248
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by mxbikerider View Post
    So the cookie is being set correctly, but when I refresh the page, the cookie is reset again to 0... I think it has something to do with this part right here..

    cookie = document.cookie.indexOf("name");
    i = cookie;

    i is where the list starts from, but it's not working!!!!
    of course it’s not working, cookie is giving you the index number in the cookie string, where "name" starts (if that is the only value in the cookie, that would be 0). you need to grab the value after name (or rather, between "name=" and the next ";")
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,773
    Thanks
    19
    Thanked 155 Times in 146 Posts
    I don't know exactly what you are doing, but this is an example of what you are trying to achieve in terms of basic functionality:

    people.xml
    Code:
    <?xml version="1.0"?>
    <people>
    	<person id="1">
    		<firstname>Tom</firstname>
    		<lastname>Smith</lastname>
    	</person>
    	<person id="2">
    		<firstname>Ned</firstname>
    		<lastname>Jones</lastname>
    	</person>
    	<person id="3">
    		<firstname>Steve</firstname>
    		<lastname>McNally</lastname>
    	</person>
    	<person id="4">
    		<firstname>Bob</firstname>
    		<lastname>Goodman</lastname>
    	</person>
    	<person id="5">
    		<firstname>Rob</firstname>
    		<lastname>Reiner</lastname>
    	</person>
    </people>
    index.html
    Code:
    <?xml version="1.1" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <title>TITLE</title>
    <style type="text/css">
    	span {color:red;}
    </style>
    </head>
    <body>
    
    <div id="personDetails">
    	<p>First Name: <span></span></p>
    	<p>Last Name: <span></span></p>
    </div>
    <input type="button" value="Next Person >>" onclick="window.location.reload();" />
    
    <script type="text/javascript">
    	function loadXml(url)
    	{
    		xmlhttp = new XMLHttpRequest();
    		xmlhttp.open("GET", url, false);
    		xmlhttp.send();
    		return xmlhttp.responseXML;
    	}
    
    	function retrievePerson(xml, personID)
    	{
    		var person = {};
    		var path = "/people/person[@id=" + personID + "]";
    
    		if (window.ActiveXObject)
    		{
    			// code for IE
    			var nodes = xml.selectNodes(path);
    			var result = nodes.nextNode();
    			//alert(result.text);
    			person.firstname = result.getElementsByTagName('firstname').item(0).text;
    			person.lastname = result.getElementsByTagName('lastname').item(0).text;
    		}
    		else
    		{
    			// code for Mozilla, Firefox, Opera, etc.
    			var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
    			var result = nodes.iterateNext();
    			//alert(result.textContent);
    			person.firstname = result.getElementsByTagName('firstname').item(0).textContent;
    			person.lastname = result.getElementsByTagName('lastname').item(0).textContent;
    		}
    
    		return person;
    	}
    
    	function createCookie(name,value,days)
    	{
    		var expires = "";
    		if (days)
    		{
    			var date = new Date();
    			date.setTime(date.getTime() + (days*24*60*60*1000));
    			expires = "; expires=" + date.toGMTString();
    		}
    		document.cookie = name + "=" + value + expires + "; path=/";
    	}
    
    	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;
    	}
    
    	window.onload = function()
    	{
    		var xmlDoc = loadXml('people.xml');
    
    		var cookieVal = readCookie('currentPerson');
    		var personID = (cookieVal == null || parseInt(cookieVal) >= xmlDoc.getElementsByTagName('person').length) ? 1 : parseInt(cookieVal) + 1;
    		var person = retrievePerson(xmlDoc, personID);
    		createCookie('currentPerson', personID.toString(), 365);
    
    		var personDetails = document.getElementById('personDetails').getElementsByTagName('span');
    		personDetails[0].innerHTML = person.firstname;
    		personDetails[1].innerHTML = person.lastname;
    	};
    </script>
    
    </body>
    </html>

    By the way, I'm using XPath to extract data from the XML file, because XPath is generally the easiest way to do so.
    Last edited by chump2877; 12-17-2011 at 06:40 AM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!


  •  

    Posting Permissions

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