1

I need to retrieve all the hyperlinks present in current html page using jquery. How can i use regular expressions to do that? Also can i use collections in javascript to store all the hrefs in the list?

1
  • i used yql to retrieve html of a link but i need to get href from that result which was returned by the yql. Instead of this.href can i pass the html string to it? Commented Jul 11, 2012 at 9:41

6 Answers 6

2

You can use simple each-loop:

// For each <a href=""> tag...
$('a').each(function(){
    // Get the url
    var href = $(this).attr('href')
    // alert it or do sth with it
    alert( href ); 
})
Sign up to request clarification or add additional context in comments.

3 Comments

i used yql to retrieve html of a link but i need to get href from that result which was returned by the yql. Instead of this.href can i pass the html string to it?
Use $(html_tring_here).find('a').each(...
Might want to use .filter() rather than .find() because .find() won't match any top-level elements.
1

using jQuery $('a') will do it for you

Iterating through all the anchor elements

var hrefArr = [];
$('a').each(function(){
  //hrefArr.push($(this).attr('href')); 
  hrefArr.push(this.href); // this would be bit faster then the statement above it
});

Edit on comment by OP, Finding the anchor tags in string contain html

str = "<div><a href='local1'></a><a href='local2'></a></div>"
var hrefArr = [];
$(str).filter('a').each(function(){
    hrefArr.push(this.href); 
});​

4 Comments

i used yql to retrieve html of a link but i need to get href from that result which was returned by the yql. Instead of this.href can i pass the html string to it?
replace $('a') with $(str).find('a') where str contains the text
Might want to use .filter() rather than .find() because .find() won't match any top-level elements in the string. (Your example string would of course work with .find() since you assumed a div as a container.)
Thanks @nnnnnn for your suggestion, changed to filter.
0

All anchors would be quite easy

$("a").each(function(){
  alert( $(this).attr("href") );
})

1 Comment

i used yql to retrieve html of a link but i need to get href from that result which was returned by the yql. Instead of this.href can i pass the html string to it?
0

This will give you all the hyperlinks on the page

$('a')

And this will give you collection of hrefs on the hpyerlinks

var hrefs = new Array();
$('a').each(function() {
    hrefs.push(this.attr('href'));
});

2 Comments

That second example will return the href attribute of the first anchor tag, not a collection.
i used yql to retrieve html of a link but i need to get href from that result which was returned by the yql. Instead of this.href can i pass the html string to it?
0

If by "collections in javascript" you mean an array then you can do this:

var hrefs = [];
$("a").each(function() {
    hrefs.push(this.href);
});
// hrefs array now contains all the hrefs

Within the .each() callback function this.href will likely return the full url that would be followed if you click the link, that is href="test.htm" would be returned as http://yourdomain/test.htm. If you want just the bit literally in the attribute use $(this).attr("href").

You wouldn't use a regex to do that type of processing.

2 Comments

i used yql to retrieve html of a link but i need to get href from that result which was returned by the yql. Instead of this.href can i pass the html string to it?
Can you pass the html string to what? I'm not very familiar with yql, but if that's what you're using why didn't you say so in your question? You asked for a jQuery solution. If we assume your yql returns the html into a string str then you could do $(str).filter("a").each(...
0

Try this:

var link = "";
$("a").each(function() {
    link += $(this).attr("href"); 
});
alert(link);

1 Comment

I'm not sure that concatenating all the hrefs together without any spaces or commas is going to help.

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.