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?
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?
$(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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With