$.url() - A simple, lightweight url parser for jQuery
github.com
github.com
This sounds like arguing that we should use getElementById&al, addEventListener, and array.length with for loops instead of $('selector').on 'foo' and .each(). While this lib's inner code leaves to be desired in a number of ways, I really appreciate the readability and the lack of need to instantiate a full-blown DOM element then read a property to obtain a simple single result or two.
URL parsing, for 90% of uses, tends to be a one-off need that doesn't really justify the addition of another library. Making decisions about what library to include is about trade-offs; this library is worth it if you need the query string sugaring or are doing lots of URL parsing. But "instantiating a full blown DOM element" to parse a single URL is over-stating the cost of the activity, especially when compared to adding an additional library to your application.
http://www.domain.com/path/index.html?query1=test&silly=willy#test=hash&chucky=cheese
$.url('?'); // query1=test&silly=willy
$.url('?silly'); // willy
$.url('?poo'); // (an empty string)
$.url('#'); // test=hash&chucky=cheese
$.url('#chucky'); // cheese
$.url('#poo'); // (an empty string) $.url('host', 'http://www.google.com.br'); // 'com.br'
$.url('sub', 'http://www.google.com.br'); // 'www.google'
$.url('tld', 'http://www.google.com.br'); // 'br'
It will work for simple cases where the TLD has only one field like (www.google.com) or (www.usa.gov), but it will fail for any other tld with country codes.The "sub" behavior is awkward, however. It would make more sense to just have a numeric way of accessing the domain parts by index, just like they do with extracting the path elements.
As for "host", well that is just completely the wrong behavior. Since the parameter is "host", you would expect that to give the hostname. But the hostname corresponding to an internet URL is simply the fully qualified domain name. I have no idea how the behavior implemented would be useful in the first place, but it certainly shouldn't be called "host".
[1] http://en.wikipedia.org/wiki/Country_code_second-level_domai...
else if($.isNumeric(arg)
There's no native JavaScript equivalent to it if I recall correctly, but there's a bunch of alternatives to it as well.It should be easy enough to extract out the logic and repackage it. Maybe I'll fork it tomorrow and do the changes myself.
isNumeric: function( obj ) {
return !isNaN( parseFloat(obj) ) && isFinite( obj );
} var file = $('a#interesting').url('file')
vs var file = url($('a#interesting'), 'file');https://github.com/component/component/wiki/Building-better-...
Anyway, I've fixed/updated a bunch of things from the comments, if anyone finds any other bugs or missing features probably best to leave me an issue on the github project page here: https://github.com/websanova/js-url/issues.
The project has also been un-jquery-ified and renamed to js-url (thanks to Jay Adkisson : https://github.com/jayferd)
Thanks
(1) https://github.com/stricaud/furl
and
(2) https://github.com/gruns/furl
furl(1) seems to handle a lot of edge cases when the URL is containing an IPv4/IPv6 address or when the URL is partial (e.g. without the protocol). furl(1) aims is to be fast. furl(2) seems to be more flexible and used for inline modification of the URL.
- Breaks for protocols other than http/https, including protocol-relative urls.
- Breaks query string values that contain '='.
The best lightweight js library I've found/used in the past is parseUri:
http://blog.stevenlevithan.com/archives/parseuri
Obviously, if you only need to analyze the current URL, you can use window/document.location, but there are times when you want to parse a URI for a page you aren't on. For instance, if you are analyzing har data, and want to break URI "components" into columns so they are sortable.
And this library doesn't have a strong reason to tie itself to jQuery. It doesn't need jQuery's DOM operation or AJAX. It's like making a program depending on third party library without actually using any of the feature.
And if you follow the project again, you will see that it has become a regular non-jQuery library.
But me being a douche aside, I'm not sure why you needed to extend from/bake into jQuery for this.