在JavaScript中解析相对URL [英] Resolving relative URLs in JavaScript
问题描述
我正在构建一个需要查看form [action]和[href]值并将其解析为绝对URL的JS库。
例如,我在 http:// a / b / c / d; p?q 并遇到../g的href值(假设没有< base>元素)。由此产生的绝对值为: http:// a / b / g 。
是否有一个JS库已经这样做?我不得不相信。
有关需要什么的更多信息,规范:
http://tools.ietf.org/html/rfc3986#section-5.4
.getAttribute('href ')
,但 .href
)会返回已解析(绝对)的网址。 I'm building a JS library which has a requirement of looking at form[action] and a[href] values and resolving them into absolute URLs.
For example, I'm on http://a/b/c/d;p?q and encounter an href value of "../g" (assume there's no <base> element). The resulting absolute would be: http://a/b/g.
Is there a JS library that does this already? I'd have to believe so.
For more info about what's needed, the spec: http://tools.ietf.org/html/rfc3986#section-5.4
It turns out that the .href
attribute of a A element (not .getAttribute('href')
, but .href
) returns the resolved (absolute) URL.
这篇关于在JavaScript中解析相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!