Socket
Socket
Sign inDemoInstall

resolve-url

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

resolve-url

Like Node.js’ `path.resolve`/`url.resolve` for the browser.


Version published
Maintainers
1
Weekly downloads
12,392,835
decreased by-11.39%

Weekly downloads

Package description

What is resolve-url?

The resolve-url package is a utility for resolving URLs based on a base URL. It is commonly used to resolve relative paths to absolute URLs in the context of web development.

What are resolve-url's main functionalities?

Resolve relative URL

This feature allows you to resolve a relative URL to an absolute URL using a base URL. In the code sample, '/some/path' is resolved against the base URL 'http://example.com/' to form the absolute URL 'http://example.com/some/path'.

var resolve = require('resolve-url');
var resolvedUrl = resolve('http://example.com/', '/some/path');

Resolve URL with query string

This feature enables resolving a relative URL in the presence of a query string in the base URL. The code sample demonstrates how 'path' is resolved against 'http://example.com/?query=123' to form 'http://example.com/path?query=123'.

var resolve = require('resolve-url');
var resolvedUrl = resolve('http://example.com/?query=123', 'path');

Resolve URL with hash fragment

This feature is used to resolve a relative URL when there is a hash fragment in the base URL. In the code sample, 'path' is resolved against 'http://example.com/#hash' to form 'http://example.com/path#hash'.

var resolve = require('resolve-url');
var resolvedUrl = resolve('http://example.com/#hash', 'path');

Other packages similar to resolve-url

Readme

Source

Overview

browser support

Like Node.js’ path.resolve/url.resolve for the browser.

var resolveUrl = require("resolve-url")

window.location
// https://example.com/articles/resolving-urls/edit

resolveUrl("remove")
// https://example.com/articles/resolving-urls/remove

resolveUrl("/static/scripts/app.js")
// https://example.com/static/scripts/app.js

// Imagine /static/scripts/app.js contains `//# sourceMappingURL=../source-maps/app.js.map`
resolveUrl("/static/scripts/app.js", "../source-maps/app.js.map")
// https://example.com/static/source-maps/app.js.map

resolveUrl("/static/scripts/app.js", "../source-maps/app.js.map", "../coffee/app.coffee")
// https://example.com/static/coffee/app.coffee

resolveUrl("//cdn.example.com/jquery.js")
// https://cdn.example.com/jquery.js

resolveUrl("http://foo.org/")
// http://foo.org/

Installation

  • npm install resolve-url
  • bower install resolve-url
  • component install lydell/resolve-url

Works with CommonJS, AMD and browser globals, through UMD.

Usage

resolveUrl(...urls)

Pass one or more urls. Resolves the last one to an absolute url, using the previous ones and window.location.

It’s like starting out on window.location, and then clicking links with the urls as href attributes in order, from left to right.

Unlike Node.js’ path.resolve, this function always goes through all of the arguments, from left to right. path.resolve goes from right to left and only in the worst case goes through them all. Should that matter.

Actually, the function is really like clicking a lot of links in series: An actual <a> gets its href attribute set for each url! This means that the url resolution of the browser is used, which makes this module really light-weight.

Also note that this functions deals with urls, not paths, so in that respect it has more in common with Node.js’ url.resolve. But the arguments are more like path.resolve.

Tests

Run npm test, which lints the code and then gives you a link to open in a browser of choice (using testling).

License

The X11 (“MIT”) License.

Keywords

FAQs

Last updated on 25 Feb 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc