Click a webpage's Next button via Javascript

Sep 20, '06 07:30:06AM

Contributed by: Anonymous

Sites such as macosxhints and the OS X page at iusethis.com, and many other great webpages, put a Next button down at the bottom of the page, so you can see the next webpage in the sequence. Sometimes, on a busy page, it's a little difficult to find. This hint gives allows you to define a bookmark that will hit that Next button automagically.

The idea is to pop the following snippet of javascript into the bookmark's address:

javascript:
var contents=document.links;
var regexp = /next/i;
for (i = contents.length - 1; i >= 0; i--) {
  possURL = regexp.exec(contents[i].text);
  if (possURL != null) {
    self.location=contents[i].href;
  }
}
Copy everything from javascript to the last }. Next, show all bookmarks, create a new bookmark (call it "Next button"), edit its address (contextual menu action), and paste the above Javascript into its address field. When you leave the address field, the formatting will disappear, and everything will be on one line, with %20 where spaces used to be. The code will still work though. If you've created the bookmark in the bookmark bar, you'll have a button at the top of every webpage to click.

How it works: The Javascript searches all links in the page, from the end to the top, looking for one with the text "next" in it. If it finds it, it loads the referenced page. I don't know Javascript, and there's probably much more that could be done in more elegant ways. It doesn't work with all pages, but that could probably be fixed too.

[robg adds: I tested this, and it works well on the macosxhints pages.]

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20060914164702685