www.webdeveloper.com
Results 1 to 3 of 3

Thread: Test for object with GetelementById

  1. #1
    Join Date
    Sep 2006
    Posts
    20

    Test for object with GetelementById

    Can someone please help?
    I am getting an error with the following code. Its doing a test to see if an element exists on a form and if it does it should be getting the value form the first array and giving it the src from the second array, the code is not in a function as I would like it to run every time..

    The site is at http://80.244.186.152/

    Any help is much appreciated.

    <script type="text/javascript" language="javascript">
    var element = new Array();
    element[0] = "top";
    element[1] = "sky";
    element[2] = "square";
    element[3] = "button1";
    element[4] = "button2";
    element[5] = "button3";
    element[6] = "button4";
    element[7] = "button5";
    element[8] = "bottom";

    var src = new Array();
    src[0] = "http://servedbyadbutler.com/adserve/;ID=152657;size=468x60;setID=104753;type=iframe";
    src[1] = "http://servedbyadbutler.com/adserve/;ID=152657;size=120x600;setID=107507;type=iframe";
    src[2] = "http://servedbyadbutler.com/adserve/;ID=152657;size=300x250;setID=104754;type=iframe";
    src[3] = "http://servedbyadbutler.com/adserve/;ID=152657;size=160x80;setID=104755;type=iframe";
    src[4] = "http://servedbyadbutler.com/adserve/;ID=152657;size=160x80;setID=104756;type=iframe";
    src[5] = "http://servedbyadbutler.com/adserve/;ID=152657;size=160x80;setID=104757;type=iframe";
    src[6] = "http://servedbyadbutler.com/adserve/;ID=152657;size=160x80;setID=104758;type=iframe";
    src[7] = "http://servedbyadbutler.com/adserve/;ID=152657;size=300x250;setID=104754;type=iframe";
    src[8] ="http://servedbyadbutler.com/adserve/;ID=152657;size=468x60;setID=105901;type=iframe";

    var str = '';
    var elem = document.getElementById('form1').elements;
    for(var i = 0; i < elem.length; i++)
    {
    if (elem[i].name==element[i].name)
    {
    document.getElementById(element[i].name).src = src[i]
    }
    }
    </script>
    Last edited by corrado100; 07-21-2008 at 11:40 AM.

  2. #2
    Join Date
    Jul 2008
    Posts
    38
    I would suggest declaring it as a function and then setting it to run 'onload' to ensure that it executes after the page finishes loading. Otherwise, I'm not sure you can garuntee that the elements you are looking for actually exist yet.

  3. #3
    Join Date
    Sep 2006
    Posts
    20
    I will have to load the code in the code behind onload as the body tag is already calling various bits of javascript and i think you are right as I will have to wrap the code in a function first.
    Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles