url()
A simple, lightweight url parser for JavaScript (~1.7 Kb minified, ~0.7Kb gzipped).
Also available for Node.js.
Bower Install
> bower install js-url
Notes
For path(1) and path(-1) will always act as if the path is in the form /some/path/
regardless of whether the original path was /some/path
or some/path/
.
Tld
There are two versions, url.min.js
and url-tld.min.jd
. The tld
version contains a list of valid tld's making the file about 2kb larger. If you don't need support for it just use domain parts arguments (url('-1')
) to get the tld pieces you need.
Examples
http://rob:abcd1234@www.example.co.uk/path/index.html?query1=test&silly=willy&field[0]=zero&field[2]=two#test=hash&chucky=cheese
; // http://rob:abcd1234@www.example.co.uk/path/index.html?query1=test&silly=willy&field[0]=zero&field[2]=two#test=hash&chucky=cheese; // co.uk; // example.co.uk; // www.example.co.uk; // www // undefined // www // example // uk // rob:abcd1234 // rob // abcd1234; // 80; // http; // /path/index.html; // index.html; // index; // html; // path; // index.html; // undefined; // index.html; // path; // index.html; // index.html; // query1=test&silly=willy; // {query1: 'test', silly: 'willy', field: ['zero', undefined, 'two']}; // willy; // null // zero // ['zero', undefined, 'two']; // test=hash&chucky=cheese; // {test: 'hash', chucky: 'cheese'}; // cheese; // undefined
Also supports mailto
.
; // mailto; // rob@websanova.com
We can also pass a url in and use all the same options on it:
; // test.www.example.com; // http; // /some/path; // 8080; // httpsetc...
jQuery
Also include is a jQuery version of the plugin that can be called via $.url() with all the same options. If you're already using jQuery it may be better to use the jQuery version to avoid namespacing issues.
Grunt.js
If you want to use Grunt you will need to install the required plugins locally using npm install
in the root folder of your project. If you need to setup Grunt on your system you can follow my Setting up Grunt.js guide.
Resources
- More jQuery plugins by Websanova
- jQuery Plugin Development Boilerplate
- The Ultimate Guide to Writing jQuery Plugins
License
MIT licensed
Copyright (C) 2011-2012 Websanova http://www.websanova.com