Why does Javascript treat relative URLs differently than standard HTML? Think of the following URL (or just browse to it): http://en.wikipedia.org/wiki/Rome.
Open a Firebug
Looks like http://code.google.com/p/js-uri/ is the library of choice for URL manipulation in general and this type of absolute-to-relative computation in particular:
new URI(potentiallyRelativeLink).resolve(new URI(window.location.href)).toString()