Removing Relative URLs in JavaScript

I am creating a JS library that has a requirement to look at the form [action] and [href] values ​​and resolve them to absolute URLs.

For example, I am on http: // a / b / c / d; p? Q and meet the href value for "../g" (suppose there is no <base> element). The resulting absolute will be: http: // a / b / g .

Is there a JS library that already does this? I had to believe it.

For more information on what you need, spec: http://tools.ietf.org/html/rfc3986#section-5.4

+3
source share
3 answers

, .href A ( .getAttribute('href'), .href) () URL-.

+8
+1

URL- :

u = (new URL("?newSearch",
             "http://a.example/with/a/long/path.file?search#fragment")).href

( http://a.example/with/a/long/path.file?newSearch)

, , :

u = (new URL("?newSearch", document.location)).href

URL URL (, , , , ..).

+1

Source: https://habr.com/ru/post/1769760/


All Articles