开发者

Get pathname from href in Javascript

whats the simplest way to return the "pathname" from an anchor tags href attribute?

example... s开发者_运维问答ay I have:

<a href="http://www.example.com/this/is/my/path.html">Blah</a>

I need to return only this "/this/is/my/path.html" part.

Ideas? I'm using jQuery if it helps..

Thanks!


I think you can use pathname

$('a')[0].pathname;


see working example here.. http://jsfiddle.net/TvNmL/

HTML..

<a id='lnk' href="http://www.example.com/this/is/my/path.html">Blah</a>

javascript...

alert( document.getElementById('lnk').pathname);


I noticed there's still no proper answer that deals with the IE bug that @Funka mentioned, so here's my solution:

HTML

<a href="/foo" id="foo">My link</a>

JS

document.getElementById("foo").getAttribute("href");

results '/foo' on all browsers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜