...

View Full Version : Set a cookie for where I left off in XML list?



mxbikerider
12-16-2011, 06:12 AM
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?

Dormilich
12-16-2011, 11:38 AM
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)

mxbikerider
12-16-2011, 05:57 PM
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.



<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>

Dormilich
12-16-2011, 11:25 PM
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 ";")

chump2877
12-17-2011, 06:31 AM
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

<?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

<?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 (http://www.w3schools.com/xpath/xpath_syntax.asp) to extract data from the XML file, because XPath is generally the easiest way to do so.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum