-1

I am creating a Tampermonkey keyboard navigation script in Javascript for a website that I use (I do not own the site so I can't make any changes to it myself). If the website has the following code:

<a href="/page/2.html">Next »</a>

to get the next page, how would I retrieve the "/page/2.html" part by searching the website for "Next"? Thank you much for your time!

Edit: I have done this before and I know what I ask is possible. The only portion of the website that is used for this code, I have listed above. I only wish to use the inner HTML "Next" and nothing else. What is shown above is exactly what is on the HTML file for the website.

Shelby V
  • 11
  • 3
  • 2
    http://stackoverflow.com/questions/1550901/how-to-get-raw-href-contents-in-javascript – tonoslfx Apr 13 '15 at 12:51
  • You need to use more of the page's structure (which you have not shown us) to construct a selector (jQuery or CSS or XPath, in that order of preference) that gets just the right kind of "Next" link. If this is for a webcomic, they range from really easy to a right PITA. (Hint, people have already written hotkey scripts for many web comics.) – Brock Adams Apr 13 '15 at 17:06
  • I have done this before, but I cannot remember how. I was able to use Javascript (not jQuery, CSS, XPath, etc) to search for the inner HTML "Next" and click if found. The only portion of the site that is needed is exactly what I listed above – Shelby V Apr 14 '15 at 02:15
  • No. Many such pages have more than one "Next" link, and they don't always do the same thing. Edit the question to conform to Stack Overflow requirements (MCVE, show effort, etc.). – Brock Adams Apr 14 '15 at 19:54

1 Answers1

0

I wouldn't recommend doing this at all, since simply typing "Next" on the page somewhere else could break the code. If the element in the page has an id attribute, just do:
document.getElementById("idOfTheElement").getAttribute("href");

But if you really want to search the page, you could do this:

function getNextHref() {
    var aTags = document.getElementsByTagName("a");
    for(var i=0; i<aTags.length; ++i) {
        if(aTags[i].search("Next") >= 0) {
            return aTags[i].getAttribute("href");
        }
    }
    return null;
}

Note that element.getAttribute("href"); returns exactly what was typed in the tag while element.href returns the interpreted full link.

Domino
  • 6,314
  • 1
  • 32
  • 58