2

got this example:

<html>
<head>
<script type="text/javascript">
    function init(){
        var linkPage = document.getElementById('linkid').href;
        window.location.href = linkPage;
    }
    onload=init;
</script>
</head>
<body>

<a href="someplace.html" id="linkid">GO HERE</a>

</body>
</html>

this script clicks the link "GO HERE". (works perfect)

but in my example i got no class or id in the link.

<a href="someplace_with_session.php&c=ToMNi1ffNs4qf55cSmaisSvv3h8NqUliyO&o=16cb29acefffyccc83bbd8e&r=338a8d7492839096df1dd">LINK NAME</a>

is only thing that never change is the name of the link ("LINK NAME")

is it possible to search for "LINK NAME" and then click it like the working script above?

or something that will do what i need :D

2
  • Why can't you give it an id? That's what they are for. Commented Feb 22, 2013 at 0:25
  • @Dennis just need the code for greasemonkey! can't add an id because the website is not mine :D Commented Feb 22, 2013 at 0:28

3 Answers 3

5

JS has no way to search for a node by text contents (that I know of).

Array.prototype.forEach.call(document.getElementsByTagName('a'), function (elem) {
    if (elem.innerHTML.indexOf('LINK NAME') > -1) {
        window.location = elem.href;
    }
});
Sign up to request clarification or add additional context in comments.

12 Comments

your code works also perfect.. just one question: it needs around 1 sec when the link gets "clicked". do you think there is an faster method?
Is there a more specific place to search for your a instead of the entire document?
Worth noting: Not all browsers in significant use have Array.prototype.forEach, you may need a shim for some. (I'm looking at you, Microsoft.)
@T.J.Crowder I like to pretend that everyone who uses IE at least uses IE9
@ExplosionPills: In non-"compatibility" mode. Ah, if only 'twere so. The heart can only aspire... But actually, the OP said something about GreaseMonkey, so you're all set.
|
2

Iterate over the links in the document and check the text:

for(var i = 0, len = document.links.length; i < len; i += 1) {
    if(document.links[i].textContent === "LINK TEXT") {
        document.links[i].click();
    }
}

6 Comments

dennis your code works perfect.. just one question: it needs around 1 sec when the link gets "clicked". do you think there is an faster method? :D
Note that textContent isn't necessarily reliable cross-browser (fall back on innerText), and one could argue that document.getElementsByTagName('a') is more well-documented than document.links. But fundamentally, this is the right answer.
Not without knowing more about the document.
@T.J.Crowder OP said it was for greasemonkey so I figured it was okay. Someone wanting to apply it to a different scenario may need that note, though. document.links is specified in DOM Level 2 and shorter than gEBTN so I don't feel bad about that one either.
@Dennis: Ah, good point! He/she did mention GreaseMonkey... (Wow, links is in DOM2? I need to re-read that... I'm rubbish on my shortcut properties...)
|
2

I'd just use the following bit, which uses jquery selection.

var link = $("a:contains('LINK TEXT')"); //get the a
var click = document.createEvent("Event"); //create event
click.initEvent("click", true, true);
link.dispatchEvent(click); // make it happen 

4 Comments

The OP has said nothing about jQuery. Please don't post jQuery-only answers to non-jQuery questions.
dude, instead of straight trolling, why not offer any real worth to the post? @T.J.Crowder.
@ Todd: Dude, asking that people actually answer the question as asked is not trolling. (I've posted 4,500+ answers, hardly troll-like behavior.) I've been around this block. jQuery answers to non-jQuery questions are not cool. Look for it on meta. Meanwhile, there's a perfectly good answer to this question already, so there was no reason for me to post one.
fair enough. Can I borrow some silvadene for that burn? @t.j. Crowder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.