views:

588

answers:

3

Is it possible to get the actual URL (as opposed to the src attribute value) of an image within the current DOM using jQuery or JavaScript?

i.e. retrieve "example.com/foo.jpg" as opposed to "foo.jpg" (taking <base> elements into account)

What about any other interesting properties such as the mime type, file size or, best of all, the actual binary data?

+5  A: 

I wonder if jQuery is using .getAttribute() - using .src always seems to give the absolute URL:

<img src="foo.jpg" id="i">

<script>
var img = document.getElementById('i');

alert(img.getAttribute('src')); // foo.jpg
alert(img.src);                 // http://..../foo.jpg
</script>

To get the rest of the information, could you use an AJAX request and look at the header & data sent?

Greg
To do this with a Jquery selector use: $(selector).get(0).src;
Pim Jager
Good thinking Greg, cheers. As for the rest, I was hoping I might be able to do something without extra AJAX round trips, given that it should already be loaded, and avoiding any cross site AJAX issues would be good too.
@Pim, or $(selector)[0]
J-P
+1  A: 

$(you IMG selector).attr('src');

for instance, if we have an *<img src='bla-bla.jpg' id='my_img' />*

then we'll do:

*$('#my_img').attr('src');*

A: 

simple

$("#my_img").src;

just it resolve the problem

Guilherme Schvarcz Franco