javascript - find specific links that contain a word and add to array -


I'm trying to find a page for links with the word playgame. If they found them, then I added them to an array, then select random values ​​from the array and use window.location . My problem is that it says that my indexoff is undefined. I'm not sure what it means that I am now learning to use this feature of Javascript.

Example of a link

  & lt; A href = "playgame.aspx? Gid = 22 9 3 & tag = ddab47a0b9ba5cb4" & gt; & Lt; Img src = "http://games.mochiads.com/c/g/running-lion-2/_thumb_100x100.jpg" & gt; & Lt; / A & gt;   

javascript

  var gameLinks = document.getElementsByTagName ("a"); If (gameLinks.href.indexOf ("playgame")! = -1) {var link = []; Links.push (gameLinks.href); Var randomHref = links [Math.floor (Math.Rendum (* * link * length)]; Window.location = randomHref; }    

My problem is that it says that my index is undefined

No indexOf , the thing on which you are calling it gameLinks is a node list You do not have the href property in the href property to see the property, you need a loop through the contents of the list eg:

 < Code> var index, href, link, random hrf, game link Number; GameLinks = Documents. GetElementsByTagName ("A"); // loop = [] via link link; {// href ============================================================================================================================================================================= Href; If (href.indexOf ("playgame")! = -1) {links.push (href); }} Random HRF = Link [Math.flur (Math.Rendum (* * links.length)]; Window.location = randomHref;              

Comments