0

given a block of html that is a var, not on the actual document:

var html_cmt = "Check out this awesome link: https://github.com/jquery/jquery-ui Check out this awesome link: https://github.com/jquery/jquery-ui Check out this awesome link: https://github.com/jquery/jquery-ui Check out this awesome link:"

I need to build an array of all links, I have the following but it's only building the first one, not all.

var hrefs = new Array();
hrefs.push( $("<div>").html(html_cmt).find('a').attr('href'));

Suggestions on how I can create an array of all the links, given html content that is not on the document just a JS var? thank you

1
  • AnApprentice. I just noticed that your html_cat variable doesn't contain any HTML tags. Is that correct? Commented Jan 12, 2012 at 21:49

4 Answers 4

5
var hrefs = [];
$("<div>").html(html_cmt).find('a').each(function(){
    hrefs.push(this.href);
    //         ^^^^^^^^^ Resolves URLs automatically. See notes
});

Another approach:

var hrefs = $("<div>").html(html_cmt).find('a').map(function(){
    return this.href;
}).toArray();

Notes

this.href will return the fully resolved UR, $(this).attr('href') will return the real attribute.

Example (assume http://localhost/dir/test.php):

this.href            == http://localhost/foo.bar
$(this).attr('href') == /foo.bar

this.href            == http://localhost/dir/test.php#doo
$(this).attr('href') == #doo

this.href            == http://localhost/file.do
$(this).attr('href') == ../file.do

this.href            == http://localhost/dir/
$(this).attr('href') == .
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, not sure by what you mean by "If you want the real attr, use $(this).attr('href')" ?
@AnApprentice I have updated my answer to elaborate both methods. Unless you want to use possibly relative URLs, I recommend to use this.href.
2

You can use ..map and .get: http://jsfiddle.net/tyggq/.

var hrefs = $("<div>").html(html_cmt).find("a").map(function() {
  return this.href;
}).get();

// find <a> elements, replace them with their hrefs, and
// convert the jQuery object to an array

The string should contain actual <a> elements though; you say it works for the first <a> element so I guess your string is different from what you posted.

1 Comment

Might want to assign the end result to a variable for clarity, but nicely done otherwise. +1
1

I did it too... just a lot slower then everyone else :)

var html = "Check out this awesome link: https://URL-1 Check out this awesome link: https://URL-2 Check out this awesome link: https://URL-3 Check out this awesome link:";

var urls = [];


do
{
    var start = html.indexOf("http");
    var end = 0;

    if (start > 0)
    {
        for (var i = start; i < html.length; i++)
        {
            if (html[i] == " ")
            {
                end = i;
                break;
            }
        }
        urls.push(html.substring(start, end));
    }

    html = html.substring(end, html.length);
}
while(start >= 0);

console.log(urls);

on jsfiddle: http://jsfiddle.net/HfvyE/

Comments

0

var links = $html_cmt.split("Check out this awesome link: ")

Comments

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.