开源软件名称(OpenSource Name):medialize/URI.js开源软件地址(OpenSource Url):https://github.com/medialize/URI.js开源编程语言(OpenSource Language):JavaScript 74.3%开源软件介绍(OpenSource Introduction):URI.js
I always want to shoot myself in the head when looking at code like the following: var url = "http://example.org/foo?bar=baz";
var separator = url.indexOf('?') > -1 ? '&' : '?';
url += separator + encodeURIComponent("foo") + "=" + encodeURIComponent("bar"); Things are looking up with URL and the URL spec but until we can safely rely on that API, have a look at URI.js for a clean and simple API for mutating URIs: var url = new URI("http://example.org/foo?bar=baz");
url.addQuery("foo", "bar"); URI.js is here to help with that. API Example// mutating URLs
URI("http://example.org/foo.html?hello=world")
.username("rodneyrehm")
// -> http://rodneyrehm@example.org/foo.html?hello=world
.username("")
// -> http://example.org/foo.html?hello=world
.directory("bar")
// -> http://example.org/bar/foo.html?hello=world
.suffix("xml")
// -> http://example.org/bar/foo.xml?hello=world
.query("")
// -> http://example.org/bar/foo.xml
.tld("com")
// -> http://example.com/bar/foo.xml
.query({ foo: "bar", hello: ["world", "mars"] });
// -> http://example.com/bar/foo.xml?foo=bar&hello=world&hello=mars
// cleaning things up
URI("?&foo=bar&&foo=bar&foo=baz&")
.normalizeQuery();
// -> ?foo=bar&foo=baz
// working with relative paths
URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/world.html");
// -> ./baz.html
URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/sub/world.html")
// -> ../baz.html
.absoluteTo("/foo/bar/sub/world.html");
// -> /foo/bar/baz.html
// URI Templates
URI.expand("/foo/{dir}/{file}", {
dir: "bar",
file: "world.html"
});
// -> /foo/bar/world.html See the About Page and API Docs for more stuff. Using URI.jsURI.js (without plugins) has a gzipped weight of about 7KB - if you include all extensions you end up at about 13KB. So unless you need second level domain support and use URI templates, we suggest you don't include them in your build. If you don't need a full featured URI mangler, it may be worth looking into the much smaller parser-only alternatives listed below. URI.js is available through npm, bower, bowercdn, cdnjs and manually from the build page: # using bower
bower install uri.js
# using npm
npm install urijs BrowserI guess you'll manage to use the build tool or follow the instructions below to combine and minify the various files into URI.min.js - and I'm fairly certain you know how to Node.js and NPMInstall with // load URI.js
var URI = require('urijs');
// load an optional module (e.g. URITemplate)
var URITemplate = require('urijs/src/URITemplate');
URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/sub/world.html")
// -> ../baz.html RequireJSClone the URI.js repository or use a package manager to get URI.js into your project. require.config({
paths: {
urijs: 'where-you-put-uri.js/src'
}
});
require(['urijs/URI'], function(URI) {
console.log("URI.js and dependencies: ", URI("//amazon.co.uk").is('sld') ? 'loaded' : 'failed');
});
require(['urijs/URITemplate'], function(URITemplate) {
console.log("URITemplate.js and dependencies: ", URITemplate._cache ? 'loaded' : 'failed');
}); MinifySee the build tool or use Google Closure Compiler:
ResourcesDocuments specifying how URLs work:
Informal stuff How other environments do things Forks / Code-borrow
AlternativesIf you don't like URI.js, you may like one of the following libraries. (If yours is not listed, drop me a line…) Polyfill
URL Manipulation
URL Parsers
URI Template
Various
AuthorsContains Code From
LicenseURI.js is published under the MIT license. Until version 1.13.2 URI.js was also published under the GPL v3 license - but as this dual-licensing causes more questions than helps anyone, it was dropped with version 1.14.0. Changelogmoved to Changelog |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论