tags:

views:

59

answers:

4

We have link to some page (with html code, on the same domain), that page have one img, which attributes title and alt are equal.

Script must open link (no showing on the page), grab the src attribute of img which title=alt, and throw the value into some variable.

Is it possible to do?

Thanks.

+1  A: 

Use jQuery: on that specific page

$(document).ready(function(){    
    var link = $('img').attr('src');
    var title = $('img').attr('alt'); // or $('img').attr('title');    
});
Digital Human
This grabs attribute but don't open a page with link. As far as I could understand OP needs open one page from another and grab something exactly there.
abatishchev
@abatishchev you are true, I've no access for external page
Happy
A: 

First this will read data from external url:

$.get('../item/test.html', function(data) {
    alert(data);
});

Does it work for you? Does it display your url?

abatishchev
yes, it alerts all the html
Happy
+3  A: 

You can do it using $.get(), .filter() and .attr(), like this:

$.get('/works', function(data) {
  var src = $('img', data).filter(function() { return this.title == this.alt; })
                          .attr('src');
  dosomethingWithIt(src);
});

This is an asynchronous operation, so that function will execute and get the src when the response comes back, it won't be available in the line of code after the $.get(). You need to continue whatever operation needs that info from within that function, so it continues working once the data comes back and is ready.

Nick Craver
@Nick Craver - you are a star :) Will try to implement in hours
Happy
+2  A: 

Hi,

$.get(link, function(data) {
  variable = $(data).find('img[title="same"][alt="same"]').attr('src');
});
HiddenChilli