SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Building Site Favorites with an XML Super Cookie : Scripting the XML User Data

Now we are ready to look at the script. Our script consists of two primary functions, AddFavorite and ListFavorites. The AddFavorite function has two arguments - the URL and title. When called, this function loads the XML favorites file, appends the new entry, and writes the file back to disk.

function AddFavorite(sURL, sTitle) {
  // Load the favorites file
  siteFavorites.load("favorites")

  // Access the document
  var oXMLDoc=siteFavorites.XMLDocument;

  // Create three elements
  var oNode=oXMLDoc.createNode(1,"SITEINFO", "");
  var oURLNode=oXMLDoc.createNode(1,"URL", "");
  var oTitleNode=oXMLDoc.createNode(1,"TITLE", "");

  // Set the text of the URL and Title
  oURLNode.text=sURL;
  oTitleNode.text = sTitle

  // Parent the URL and Title to the SITEINFO element
  oNode.insertBefore(oURLNode,null)
  oNode.insertBefore(oTitleNode,null)

  // Add the new SITEINFO block to the favorites XML file
  oXMLDoc.documentElement.insertBefore(oNode, null);

  // Save the new file to disk
  siteFavorites.save("favorites")	
}

Using this function is extremely simple. Below we have a button for adding different web pages to your Site Favorite List:


After adding files to your favorites, you can view them using the ListFavorites() function. The ListFavorites() function outputs the contents of the favorites XML file in a new window:

function ListFavorites() {
  // Open a new window and display the header
  var wList = window.open("","fav","width=300,height=200,top=0,left=0,scrollbars=yes")
  wList.document.open()
  wList.document.write("<TITLE>SiteExperts.com Favorite List</TITLE>")  
  wList.document.write("<BASE TARGET=\"_blank\">")  

  // Load the file
  siteFavorites.load("favorites")

  // Get the root node
  var oXMLDoc = siteFavorites.XMLDocument.documentElement

  // Walk through each SITEINFO block
  for (var i=0;i<oXMLDoc.childNodes.length;i++) {
     // Output the URL and text as a hyperlink
     with (wList.document) {
      write("<A HREF=\"" + oXMLDoc.childNodes[i].childNodes[0].text + "\">")
      write(oXMLDoc.childNodes[i].childNodes[1].text)
      write("</A><BR>")
    }
  }
  wList.document.close()
}

To view the raw favorites file click on the button below. The file will be displayed below (use the refresh button to update).


These favorites are now persisted on your machine. Your favorites are remembered even if you close the browser, reopen it, and view this page again.

This provides you with an overview on persisting user-data with XML. You should be aware that the created XML file is only available to pages from the same domain. Therefore, for this example to be universally useful across your entire site you would need to do a little extra work to ensure that all file manipulations occur from the same directory. In addition, a simple management screen should be created for deleting and reordering items in the favorites. Both of these exercises are beyond the scope of this introduction.

Discuss and Rate this Article

Page 1:Building Site Favorites with an XML Super Cookie
Page 2:Scripting the XML User Data