admin管理员组文章数量:1386681
I'm trying to dynamically determine whether I'm on a certain page, and set a style on any link linking to itself (for a navigation menu).
The part that's catching me up is how to determine that the page is the current page.
I know I can get window.location, and pare it to the href of any links, but there tiered folders, some of which have files named the same way, and I can't rely on setting a base server url.
Basically I need the value that you get when you hover your mouse over a link, with all the relativity of the href attribute applied to the current location. I'm not really sure how to do that, though.
I'm trying to dynamically determine whether I'm on a certain page, and set a style on any link linking to itself (for a navigation menu).
The part that's catching me up is how to determine that the page is the current page.
I know I can get window.location, and pare it to the href of any links, but there tiered folders, some of which have files named the same way, and I can't rely on setting a base server url.
Basically I need the value that you get when you hover your mouse over a link, with all the relativity of the href attribute applied to the current location. I'm not really sure how to do that, though.
Share Improve this question asked Oct 7, 2011 at 17:31 DamonDamon 10.8k17 gold badges91 silver badges146 bronze badges 1- could you provide some examples? i'm still not sure what you want exactly. – Andy Commented Oct 7, 2011 at 18:21
2 Answers
Reset to default 4This one works best cross browser
//Resolve absolute url's from relative ones
function qualifyURL( url ){
var img = document.createElement('img');
img.src = url; // set string url
url = img.src; // get qualified url
img.src = null; // no server request
return url;
}
based on drew's answer, i tested this
function resolveUrl( url ){
var a = document.createElement('a');
a.href=url; // set string url
url = a.href; // get qualified url
return url;
}
and it seems to work for ie8 and up. ie7 doesnt resolve anything.
*-pike
本文标签: javascriptHow to get absolute path from a relative url with no outside infoStack Overflow
版权声明:本文标题:javascript - How to get absolute path from a relative url with no outside info? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744568124a2613174.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论