如何从没有外部信息的相对网址获取绝对路径? [英] How to get absolute path from a relative url with no outside info?
问题描述
我正在尝试动态确定我是否在某个页面上,并在链接到自身的任何链接上设置样式(对于导航菜单)。
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.
我知道我可以获得window.location,并将其与href相比较任何链接,但有分层文件夹,其中一些文件名称相同,我不能依赖设置基本服务器URL。
I know I can get window.location, and compare 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.
基本上我需要将鼠标悬停在链接上时获得的值,href属性的所有相关性都应用于当前位置。不过我真的不知道怎么做。
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.
推荐答案
根据画得的答案,我测试了这个
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;
}
它似乎适用于ie8及以上。 ie7没有解决任何问题。
and it seems to work for ie8 and up. ie7 doesnt resolve anything.
* - pike
*-pike
这篇关于如何从没有外部信息的相对网址获取绝对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!