A sample on how to use the SetCookieEx function

Michael Schwarz on Friday, July 16, 2004
Labels

A lot of pages in my intranet application are using now the SetCookieEx functions. One of my .HTC components will be similar to the System.Collections.CollectionBase object in .NET. You can add this .HTC component to your page and use it f.e. for a shopping cart.

<PUBLIC:COMPONENT ID=_collection Name="CollectionBase"> <PUBLIC:PROPERTY NAME="count" VALUE="0" /> <PUBLIC:METHOD NAME="createItem" /> <PUBLIC:METHOD NAME="item" /> <PUBLIC:METHOD NAME="add" /> <PUBLIC:METHOD NAME="indexOf" /> <PUBLIC:METHOD NAME="insert" /> <PUBLIC:METHOD NAME="remove" /> <PUBLIC:METHOD NAME="removeIndex" /> <PUBLIC:METHOD NAME="contains" /> <PUBLIC:METHOD NAME="clear" /> <PUBLIC:METHOD NAME="save" /> <PUBLIC:METHOD NAME="load" /> </PUBLIC:COMPONENT> <SCRIPT LANGUAGE="JSCRIPT">

// // all other collection functions are left out //

function save(name) { SetCookieEx(name, items) }

function load(name) { var result = GetCookieEx(name);

if(result == null || typeof(result.sort) != "function") result = new Array();

items = result; count = items.length; }

</SCRIPT>

Add this .HTC component to your page using the BEHAVIOR style and start using it!

<SCRIPT LANGUAGE="JSCRIPT">

function AddToCart(isbn, title, price) { var item = new Object();

item.isbn = isbn; item.title = title; item.price = price;

cart.add(item);

cart.save("ShoppingCart"); }

function ShowCart() { var sum = 0; var html = "";

for(var i=0; i<cart.count; i++) { var item = cart.item(i); sum += item.price;

html += "<tr><td>" + item.title + "</td><td>" + item.price + " EUR</td></tr>"; }

html += "<tr><td>&nbsp;</td><td>SUM = " + sum + " EUR</td></tr>";

// show the cart in a SPAN or DIV element using .innerHTML

}

function window::onload() { cart.load("ShoppingCart"); }

</SCRIPT>