Jump to content

click a link based on it's href attribute


next

Recommended Posts

How can i click a link based on where it redirects you?

For instance i want to loop through all links on a page and make a click only if text "Add=1" is a part of href.

for(var i=0; i<=document.links.length; i++)
if(document.links.search['Add=1'])
	alert(document.links[i].href);

i tried this, but it's not working.

 

Thanks.

<script type="text/javascript">
   window.onload = function()
   {
      var links = document.getElementsByTagName('a');
      var goodLinks = new Array();

      for(var i = 0; i < links.length; i++)
      {
         if(links[i].href.slice(-5) === "add=1")
         {
            goodLinks.push(links[i]);
         }
      }

      //goodLinks is now an array with all <a> elements whose href attribute ends in add=1
   }
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.