在JavaScript中解析相对URL [英] Resolving relative URLs in JavaScript

查看:130
本文介绍了在JavaScript中解析相对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个需要查看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

.href 属性(不是 .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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆