HTML:
<a href="/">1</a> // link to http://site.com
<a href="/section/page/">2/a> // link to http://site.com/section/page/
<a href="http://site.com/">3</a>
<a href="../gallery/1/">4</a> // link to http://site.com/gallery/1/
JS:
$("a").live('click', function(){
var url = $(this).attr("href");
//do something
});
How to convert relative path (var url) to absolute by jQuery?
Script should do nothing, if it is already an absolute path.
Thanks.
I’m pretty sure that if you use the
hrefproperty instead of getting the attribute, you’ll have a full url with domain:As noted in the question linked by @Phrogz, it sounds as though there are issues with IE6.
If you need to support it, you may need to build the
hreffrom the different parts likethis.hostandthis.pathname. Those properties are supported by IE6. There are others you could use too, but you’d need to verify support.jquery live() function deprecated in version 1.7 and removed from 1.9 so use alternate on():