Since `scripts` is a `NodeList`, you probably want `scripts.item(i)` here (and below).