views:

88

answers:

1

Using javascript, is there a standard way to get the absolute path of an image, as

var src= img.getAttribute("src");

only returns the @src tag as it was declared in the HTML

thanks, Pierre

+6  A: 

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"
meder
To clarify - `.src` actually does convert relative paths to absolute. Example: `var a = new Image(); a.src = '/a/../b'; alert(a.src)` will resolve correctly.
Jamie Wong
Thanks, that was so obvious :-))
Pierre