开发者

How to get absolute path from a relative url with no outside info?

开发者 https://www.devze.com 2023-04-11 18:13 出处:网络
I\'m trying to dynamically determine whether I\'m on a certain page, and set开发者_StackOverflow社区 a style on any link linking to itself (for a navigation menu).

I'm trying to dynamically determine whether I'm on a certain page, and set开发者_StackOverflow社区 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 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.

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.


This 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

0

精彩评论

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

关注公众号