TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

$.url() - A simple, lightweight url parser for jQuery

98 pointsby rduchnikover 12 years ago

15 comments

bitsmover 12 years ago
Just FYI, most, if not all, of this is built into javascript already, via the DOM. <a href="http://www.joezimjs.com/javascript/the-lazy-mans-url-parsing/" rel="nofollow">http://www.joezimjs.com/javascript/the-lazy-mans-url-parsing...</a>
评论 #4707879 未加载
评论 #4707872 未加载
dudusover 12 years ago
Extracting Host, Subdomain and TLD from a url is hard. And they clearly oversimplified it.<p><pre><code> $.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' </code></pre> 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.
评论 #4708209 未加载
评论 #4709200 未加载
prezjordanover 12 years ago
I like, but I'm curious why this is a jQuery plugin.
评论 #4707718 未加载
评论 #4707859 未加载
评论 #4708594 未加载
tjholowaychukover 12 years ago
this has nothing to do with jquery, stop the madness! haha<p><a href="https://github.com/component/component/wiki/Building-better-components" rel="nofollow">https://github.com/component/component/wiki/Building-better-...</a><p><a href="https://github.com/component/url" rel="nofollow">https://github.com/component/url</a>
rduchnikover 12 years ago
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.<p>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: <a href="https://github.com/websanova/js-url/issues" rel="nofollow">https://github.com/websanova/js-url/issues</a>.<p>The project has also been un-jquery-ified and renamed to js-url (thanks to Jay Adkisson : <a href="https://github.com/jayferd" rel="nofollow">https://github.com/jayferd</a>)<p>Thanks
adulauover 12 years ago
Parsing url is difficult and prone to errors. How $.url() compares to furl? There are two furl(s) on github:<p>(1) <a href="https://github.com/stricaud/furl" rel="nofollow">https://github.com/stricaud/furl</a><p>and<p>(2) <a href="https://github.com/gruns/furl" rel="nofollow">https://github.com/gruns/furl</a><p>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.
评论 #4708627 未加载
Jareover 12 years ago
Nice little utility, just a couple of issues that should be easy to fix:<p>- Breaks for protocols other than http/https, including protocol-relative urls.<p>- Breaks query string values that contain '='.
septerrover 12 years ago
Nice. And prezjordan has a good question. Why is it a jquery plugin?
BinaryBulletover 12 years ago
As others have suggested, jQuery is overkill for this.<p>The best lightweight js library I've found/used in the past is parseUri:<p><a href="http://blog.stevenlevithan.com/archives/parseuri" rel="nofollow">http://blog.stevenlevithan.com/archives/parseuri</a><p>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.
评论 #4708059 未加载
Hovertruckover 12 years ago
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.
vhiremath4over 12 years ago
I have OCD about code cleanliness... No semicolon at the end of line 26? Please excuse me as I proceed to gag.<p>But me being a douche aside, I'm not sure why you needed to extend from/bake into jQuery for this.
solox3over 12 years ago
If the url doesn't contain the required component, it will throw errors. Why not return an empty string like urlparse does?
wslhover 12 years ago
How it compares against <a href="http://code.google.com/p/jsuri/" rel="nofollow">http://code.google.com/p/jsuri/</a> ?
评论 #4708282 未加载
barefootover 12 years ago
Thanks for having the link go directly to examples.
languagehackerover 12 years ago
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.
评论 #4707960 未加载
评论 #4707970 未加载