View Full Version : Help with code

05-02-2006, 02:19 AM
The following is a piece of code a friend came up with, but apparently it's imperfect. I'm writing a chooseyourownadventure style story, and the idea is you pick up these letters/maps/books as you go along to help fill in the story. Obviously being non-linear, this style means you'll be picking up the items in a non-specific order. You also don't have to pick them up, but when you do, they should obviously stay there.

The problem occurs in IE when it says something about an "unspecified variable". Can anybody see what's going wrong?


<script type="text/javascript">
var items = new Array();
var arrayPosition = 0;
inventory.document.write('Inventory:<br />');

function addItem(newItem) {

// check item isn't already in inventory
var i = 0;
for(i=0; i<items.length; i++)
if(newItem==items[i]) {

// otherwise add item to array
items[arrayPosition] = newItem;


function displayInventory() {
// new window
if(window.focus){ inventory.focus() }
inventory.document.write('Inventory:<br />');

// print array
for(i=0; i<items.length; i++) {
inventory.document.writeln(items[i] + '<br />');



<a href="javascript:if(inventory){inventory.close()};addItem('letter1')">Letter 1</a>

<p /><a href="javascript:if(inventory){inventory.close()};addItem('letter2')">Letter 2</a>


05-02-2006, 05:26 AM
document.write overwrites the entire web page if it is called after the page finishes loading. To update a page after loading you need to either use innerHTML or the standard DOM calls.