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
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Script yielding inconsistent results

    Code:
    var arr = document.getElementsByTagName('iframe');
    var i, len = arr.length;
    for ( i = 0; i <= len; i++ ) {arr[i].parentNode.removeChild( arr[i] );}
    The problem is it does not work on all the elements on the page.
    It removes some of them on a random base and then gives this error: "arr[i] has no properties".
    I did an experiment with the script - added it as a bookmarklet, which showed that executing multiple instances of it does remove all elements. Question is why doesn't one run do all the work?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Start removing them from the end first. Reverse your loop [length-1 to zero]

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]


  •  

    Posting Permissions

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