views:

55

answers:

2

In jQuery you can do :

$("a[href$='.img']").each(function(index) {
    alert($(this).attr('href'));
}

I want to write a jQuery function which crawls x-levels from a website and collects all hrefs to gif images.

So when I use the the get function to retrieve another page,

$.get(href, function(data) {

});

I want to be able to do something like

data.$("a[href$='.img']").each(function(index) {
});

Is this possible ?

...UPDATE...

Thanks to the answers, I was able to fix this problem.

function FetchPage(href) {
    $.ajax({
        url: href,
        async: false,
        cache: false,
        success: function(html){
            $("#__tmp__").append("<page><name>" + href + "</name><content>" + html + "</content></page>");
        }
    });
}

See this zip file for an example how to use it.

+4  A: 

you could put recived data into the DOM and then run

$("a[href$='.img']").each(function(index) {
    alert($(this).attr('href'));
}

something like this

$.get(href, function(data) {
  $("#somelement").hide();
  $("#somelement").html(data);
  $("#somelement").find("a[href$='.img']").each(function(index) {
    alert($(this).attr('href'));
  }
  $("#somelement").show();
}
Maksim Burnin
+1  A: 

I think it's better do the crawling on the client-side (i.e. PHP) instead and return the image hrefs back to javascript/jquery.

Oh, and data.$("a[href$='.img']") is not possible. jQuery() works by searching through the currently existing DOM.

Shiki