views:

347

answers:

2

I am trying to get the link that contains a word on a page and refresh them to another page, in a bookmarklet.

Here is the code I am not getting to work for a bookmarklet:

   javascript:
        for (i=0; i < document.links.length; i++) {
     if(document.links[i].href.match('exampleword')) {
    location.href = 'http://google.com/exampleword'; 
}
}

Why is this not working?

+1  A: 

You're checking if the link href contains the word - from your question it sounds more like you want the anchor text (i.e. the words you click on). The easiest way (though not 100% accurate if the <a> contains other tags) is like this:

if (document.links[i].innerHTML.match('exampleword'))
Greg
Wouldn't it be better to search for innerText || textContent?
J-P
Oh I see. How can I display the link then: location.href = document.links[i].href?
sorry I'm a newb at javascript
+1  A: 

Greg was sharp to catch that you really want the link "label", rather than the href URL. But I would grab all of the elements with getElementsByTagName('A'). Then iterate through their firstChild nodes.

It's just as easy to pick up the 'nodeValue' of the 'firstChild', as it is to get the innerHTML or (innerText, which someone mentioned, which is less supported).

Here's a minimal example, looking for 'word1', 'word2', and 'word3'.

I will just add, that if you want to limit the links looked at for this to a subset of the links on the page or site, you could differentiate in other ways, by setting a particular className to the links you want to search. It would just complicate matters a bit to check the className of the links you grab. Herewith, a simple A Node iteration:

EXAMPLE:

<html>
<head>
<title>JavaScript match link label</title>
<script type="text/javascript">
var keywords = ['word1','word2','word3'];
function linklabels() {
    var aels = document.getElementsByTagName && document.getElementsByTagName('A');
    var aelsCt = aels.length;
    var keywordsCt = keywords.length;
    for (var i = 0; i < aels.length; i++) {
     var v= aels[i].firstChild.nodeValue;
      //this is the link label, the text seen as the link
     for (var j=0; j < keywordsCt; j++) {
      var re = new RegExp(keywords[j]); 
      if (re.test(v)) {
       alert('refreshing to http://google.com/' + v);
       //window.location.href = "http://google.com/" + v;
      }
     }
    }
}
window.onload=linklabels;
</script>
</head>
<body>
<p><a href="#">word1</a> | <a href="#">word2</a> | <a href="#">word3</a>
<br /><a href="#">word1</a> | <a href="#">word2</a> | <a href="#">word3</a></p>
</body>
</html>
Fran Corpier
Using firstChild is okay, as long as all of your links are marked-up consistently. In some situations, for example when you have nested inline tags (<strong>, <span>, etc.), it might be better to capture all textual content using innerText || textContent.
J-P
@J-P, excellent point. Going with the node methodology, I would alter the above to dig down through any children of each A tag element for text nodes (nodeType 3) before grabbing the nodeValue. (Fortunately, I built and use a function that does all of that for me in the background.) Thank you!
Fran Corpier