Edit: it’s been ages since I’ve ran into this "issue" back then, and by now it’s even simpler
for (const link of links) { }
I was working on a script for Greasemonkey (FX7) trying to remove certain links and found out that for some reason one that was present in the source, not hidden or constructed by JS, didn’t show up in the array that that function returns.
If that one would have been constructed via JS upon running that page it wouldn’t wonder me but it’s sitting right behind another link that gets found.
So has anyone an idea why this is happening and how I could work around it?
var links = document.getElementsByTagName("a");
for (var l in links){
if (links[l].href == "blah"){ ... }
}
Thats how I was trying to work with them, a bit cut down as I had some more checks to not run into nulls and such.
On a sidenote: I was wondering why that function also returns null entries at all.
Edit: I passed this problem long since I asked for help and found a nice way to do it:
for (var i = 0, l; l = links[i]; i++) { }
This keeps setting l to the current link until there aren’t any left. Works nice.
The for…in statement loops through the properties of an object. In this particular case you are iterating over Array object properties. Try to use this script instead: