How do I get link URL (not file URL or attachment URL) of an attached image [duplicate]

You could do it with jQuery fairly simply.

$("#caption").src($("img").src);

is psuedo code to get you thinking down the right track. Basically, loop through the images, grab the source, and apply it to the element that you want to gain the link.