tags:

views:

84

answers:

5

I'm trying to select the first link on a page after the page has completely loaded and redirect the page to the link on the first page.

I understand how to use a selector, but I'm stumped as to how I can pick up the first item after the page loads. I'm assuming once I have the value I could use

window.location = "http://www.google.com/"

To then take the user to proper page once I have the dynamic link's value.

+3  A: 

For your selector, use a:first. This will give you the first anchor tag on the page. You can then set the location to the href attribute of the anchor tag.

Something like:

location = $("a:first").attr("href");
Kamikaze Mercenary
+4  A: 
jQuery(document).ready(function() {
   window.location = jQuery("a[href]:first").attr("href");
});

A selector with the :first pseudo-class selects the first matched element. For more information on selectors, check out jQuery's excellent documentation.

EDIT

Actually ran my code and noticed that it was returning an empty string. This was because it was picking up the first a tag which happened to not have an href attribute. I've changed the selector to only select a tags with an href attribute.

Vivin Paliath
I wonder if it wouldn't be better to use the chained method `.attr('href')` in this case, just because he's learning jQuery. I realize that the href attribute is going to be there since it's a link, but he could also use the same concept on another element if the attr is there. (mostly a for @Ryan comment, not so much @Vivn Paliath, but a good comment for commentary)
drachenstern
@drachenstern - I think you caught me in the middle of an edit - I changed it to `attr("href")` for the exact same reason that you provided :)
Vivin Paliath
@Vivin ~ Haha, nice ... don't you hate when others catch you mid-edit? ;) ...
drachenstern
Wont' work if there's an element that looks like <a href="#bookmark">
jeffkee
+1  A: 

Are you using this syntax by chance in a script tag in your page?

$(document).ready(function(){
   // Your code here
 });
drachenstern
A: 
$(document).ready(function(){
  $('a').each(function(){
    if($(this).attr('href').indexOf('http')==0) {
      window.location = $(this).attr('href');
    }
  });
});

This code will work because: It makes sure the page doesn't take a static anchor tag () and mistake it for a link. It will only use that link if it starts with an http: and if not, it goes over to the next occurrence of the tag.

jeffkee
@jeffkee ~ you missed an `'` in your code ...
drachenstern
hehe.. and I also had the wrong brackets. Updated! And your addendum below about the document ready function is perfect. Shouldn't forget that...
jeffkee
FYI The above methods that pick out the 'a[href]' won't work because some links have '#' in them for whatever reason.
jeffkee
+2  A: 

I'll point out that redirects are easily defeated. Don't rely on this for anything "secure."

You may also find that you need the first anchor that actually has an href:

$("a[href]:first").attr("href");
AutoSponge
There's no need to specify the 0-th element and in fact, that might cause problems if your wrapped set contains no elements? I would rewrite that as $("a[href]:first").attr("href");
Kamikaze Mercenary