Agreed, this is the way to do it. The only caveat is that IE has a bug where it omits the leading / on the pathname. You can normalize it with a simple one-liner: http://stackoverflow.com/a/6168370/172322
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.
Not the same thing. jQuery is worth it if you're doing lots of DOM element selection. If the entirety of your usage of jQuery is a single $("#foo").bind(...) call, then it's probably not worth loading the entire library for that.
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.
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.
That is partially incorrect. There is no such thing as a TLD with more than one field. In "http://www.google.com.br, "br" is, in fact, the TLD, while "com" is the SLD[1]. So the 'tld' behavior is fine.
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".
>I loathe this pedantry worlds more than I loathe pg's observed "midbrow dismissal".
It's not pedantry. The feature is called "tld", and its functionality should reflect the actual definition of a TLD. Since dudus' comment also contained useful criticism (which I was careful to point out and expand on), it is entirely plausible that the creator of this project (or someone else doing something with TLDs) might take all of it to heart.
Since the exact definition of a TLD is (as this discussion shows) a bit subtle and potentially confusing, it would be a Bad Thing for people to start implementing behaviors where the wrong definition was used. It would muddy the waters more and lead to more confusion about the precise meaning of TLD in the future.
>This is probably, and I'm writing this confidently and without exaggeration, the worst experience one can have socially. I've fantasized about killing pedants, and I am not ashamed to admit it.
My comment was constructive and I was careful not to be mean about it. Your comment was pointless, mean, and actually threatening. Maybe you should do a little self-examination here?
Looking at the source, it looks like there's only one line that calls jQuery on line 37 (and I don't think it should be using the short calling convention without a wrapper -- if you rename jQuery to '_' or something else it won't work):
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.
No clue, but it's nice to have a namespace for it, rather than mucking up the global context with a simple utility method. Might be better off shimmying up to underscore (perhaps only if it's already detected, otherwise go global?), however there are lots of similar jquery 'plugins' of this nature..
Well I really wasn't expecting this much feedback for this little library. I threw it up for kicks and went out for a movie, when I came back I was surprised to see all the interest. It's just a simple parser I wrote for my projects, so it doesn't get too crazy with exact definitions of "tld" and support for IPv4/IPv6 addresses as mentioned in the comments. Might be better to create a secondary more heavyweight version for things like that.
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)
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.
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.
It looks like this has been moved and is no longer a jQuery plugin, which is awesome. It's now, however, creating a global variable named `url` which is dangerous to say the least.
I would be interested too to see a comparison with jsuri.
I was about to say that jsuri is not maintained anymore but looking closer on http://code.google.com/p/jsuri/ I saw that now the project is active on github: https://github.com/derek-watson/jsUri with the latest version being 1.2.2
There's also an attempt I made, https://github.com/theozaurus/jsurlify which tries to keep the usage as terse as possible. It would be great to see what strengths / weaknesses all these different libraries have and try to join efforts where sensible.
Good job. JavaScript has needed something like this for a while. I'm still kind of confused as to why they don't have good native support for URL parsing.