...

View Full Version : Help with code



IInsanity
05-02-2006, 01: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?



<html>
<head>

<script type="text/javascript">
<!--
var items = new Array();
var arrayPosition = 0;
inventory=window.open('','inventory',width=50,height=50,left=100,top=100);
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]) {
displayInventory();
return;
}
}

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

displayInventory();
}

function displayInventory() {
// new window
inventory=window.open('','inventory',width=50,height=50,left=100,top=100);
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 />');
}
}

//-->
</script>

</head>
<body>



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

</body>
</html>

felgall
05-02-2006, 04: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum