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.

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.