Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Help with code

  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with code

    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>

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •