getting the absolute path of a <img/>

Pierre picture Pierre · Aug 16, 2010 · Viewed 10.2k times · Source

Using Javascript, is there a standard way to get the absolute path of an image? img.getAttribute("src") only returns the src attribute as it was declared in the HTML.

Answer

meder omuraliev picture meder omuraliev · Aug 16, 2010

Just do .src.

$('img')[0].src = '/images/foo.gif'
"/images/foo.gif"
$('img')[0].src
"http://stackoverflow.com/images/foo.gif"
$('img')[0].getAttribute('src')
"/images/foo.gif"