简体   繁体   中英

How to get an img url using jQuery?

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?

ie 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');

simple

$("#my_img").src;

just it resolve the problem

var img = $('img');
alert(img.attr('src')); // foo.jpg
var fullpath = window.location.host + img.attr('src');
alert(fullpath); // http://example.com/foo.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM