Search code examples
javascript

javascript - get all anchor tags and compare them to an array


I have been trying forever but it is just not working, how can I check the array of urls I got (document.getElementsByTagName('a').href;) to see if any of the websites are in another array?


Solution

  • var linkcheck = (function(){
      if(!Array.indexOf){
        Array.prototype.indexOf = function(obj){
      for(var i=0; i<this.length; i++){
        if(this[i]===obj){
          return i;
        }
      }
          return -1;
        }
      }
      var url_pages = [], anchor_nodes = []; // this is where you put the resulting urls
      var anchors = document.links; // your anchor collection
      var i = anchors.length;
      while (i--){
        var a = anchors[i];
        anchor_nodes.push(a); // push the node object in case that needs to change
        url_pages.push(a.href); // push the href attribute to the array of hrefs
      }
      return {
        urlsOnPage: url_pages, 
        anchorTags: anchor_nodes, 
        checkDuplicateUrls: function(url_list){
          var duplicates = []; // instantiate a blank array
          var j = url_list.length;
          while(j--){
            var x = url_list[j];
            if (url_pages.indexOf(x) > -1){ // check the index of each item in the array.
              duplicates.push(x); // add it to the list of duplicate urls
            }
          }
          return duplicates; // return the list of duplicates.
        },
        getAnchorsForUrl: function(url){
          return anchor_nodes[url_pages.indexOf(url)];
        }
      }
    })()
    // to use it:
    var result = linkcheck.checkDuplicateUrls(your_array_of_urls);
    

    This is a fairly straight forward implementation of a pure JavaScript method for achieving what I believe the spec calls for. This also uses closures to give you access to the result set at any time, in case your list of urls changes over time and the new list needs to be checked. I also added the resulting anchor tags as an array, since we are iterating them anyway, so you can change their properties on the fly. And since it might be useful to have there is a convenience method for getting the anchor tag by passing the url (first one in the result set). Per the comments below, included snippet to create indexOf for IE8 and switched document.getElementsByTagName to document.links to get dynamic list of objects.