Get Image Full Source

Grab the full source of an image even if the src attribute is relative. Works with or without jQuery, although you'd have to change the function calls slightly.

by BradleyStaples

HTML

<img class="one" src="../test/image1.jpg" />
<img class="two" src="/test/image2.jpg" />
<img class="three" src="image3.jpg"

JavaScript

var getSrc = function(imgSource) {
    var img = new Image();
    img.src = imgSource;
    return img.src;
};

var src1 = $(".one").attr("src"),
    src2 = $(".two").attr("src"),
    src3 = $(".three").attr("src");

document.write('src1 = ' + getSrc(src1) + '<br />src2 = ' + getSrc(src2) + '<br />src3 = ' + getSrc(src3));