Comments on: Parsing URLs with the DOM! https://j11y.io/snippets/parsing-urls-with-the-dom/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Supersha https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-717 Tue, 03 Nov 2009 12:44:12 +0000 https://j11y.io/?p=572#comment-717 Hi,James,I found sth error in your parseURL method,if uses your method in IE,the “file” property will return a empty string like that:”http://www.jsparadise.cn:8080/index.html?id=123&pages=4″.
so I change the RegExp like that:
“file: (a.pathname.match(//?([^/?#]+)$/i) || [,”])[1]”,it will solve this problem. ^_^

]]>
By: James https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-716 Wed, 22 Apr 2009 22:22:11 +0000 https://j11y.io/?p=572#comment-716 Interesting! Thanks for the tests Paul! 🙂

]]>
By: Paul Irish https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-715 Wed, 22 Apr 2009 22:01:57 +0000 https://j11y.io/?p=572#comment-715 I finally got around to benchmarking this against Steven’s parseUri.

Test code:

var start = +new Date, x = 2000;
while(--x){
  parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
}
console.log('parseURL: ',+new Date - start);
 
 
var start = +new Date, x = 2000;
while(--x){
  parseUri('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
}
console.log('parseUri: ',+new Date - start);

Results
Firefox 3:
parseURL: 541
parseUri: 157

IE 6:
parseURL: 781
parseUri: 312

Chrome 1:
parseURL: 498
parseUri: 242

So the regex is faster, but I’m still quite fond of the DOM approach. :-]

]]>
By: James https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-714 Thu, 26 Feb 2009 07:53:11 +0000 https://j11y.io/?p=572#comment-714 Thanks for the comments. 🙂

Like almost everything I write about, this was just a bit of an experiment. Feature-wise it certainly does not match up to Steven’s work.

@Steven, thanks for the correction on the tp/tps thing. For my test I used a typical scenario – waiting to retrieve all the parts of a URL. It’s worth mentioning that I didn’t test against your original version; I tested against Mark’s jQuery port which was very slow in comparison to my above attempt. Although, like you said, the results are invalidated because of the inherent differences of the returned results.

@Mark, Oddly Steven’s original version is faster than yours because it returns all the information as properties while yours requires each bit of info to be initiated as a method in order to retrieve results. Doing this is probably adding overhead.

]]>
By: Sanbor https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-713 Wed, 25 Feb 2009 18:22:11 +0000 https://j11y.io/?p=572#comment-713 You’re a javascript rockstar ! 😛

]]>
By: Steven L. https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-712 Wed, 25 Feb 2009 05:01:12 +0000 https://j11y.io/?p=572#comment-712 @Mark Perkins, re: @Paul Irish’s question, I suspect “jQuery URL Parser” adds performance overhead on top of parseUri, but in any case, I’m interested in the actual test behind James’s performance assertion. E.g., if the test is done in parseUri’s loose parsing mode, that’s apples to oranges because this function offers no “loose” mode, nor does it do any processing to extract user info from a URL, etc. I expect using the DOM like this to be faster, regardless, but we’re talking about the difference between two fast functions that are unlikely to ever present a real bottleneck.

I played around with this DOM-based approach a couple years ago based on a comment on my parseUri post, but I don’t think this approach can offer all the same functionality (strict vs. loose parsing, all parts being optional, etc.) in comparable code length. Note that this function will incorrectly (IMO) return the protocol, host, port, and path of the page running the script if those segments are not included in the URI passed to the function (e.g., with parseURL(“?q=v”)).

@James Padolsey, the regex used for the “relative” property currently only works with protocols that end with “tp” (e.g., not “https”).

]]>
By: Elijah Grey https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-711 Wed, 25 Feb 2009 02:25:47 +0000 https://j11y.io/?p=572#comment-711 location.host !== location.hostname (location.host includes any non-normal ports) You should replace line 11 with this:

host: a.host,
hostname: a.hostname,
]]>
By: Mark Perkins https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-710 Tue, 24 Feb 2009 10:39:17 +0000 https://j11y.io/?p=572#comment-710 James –

Great work. Never occurred to me that you could use the parse a URL in this way, and I have to say it is a much neater implementation (and faster too, as you say!) than my plugin. Good stuff! Looks like it’s back to using someone elses code for my url parsing 🙂

@Paul Irish – I would imagine that the speed difference would be about 12 times, as Stephens parseUri function forms the core of my jQuery URL Parser plugin that James’ comparison references.

]]>
By: James https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-709 Sun, 22 Feb 2009 20:38:07 +0000 https://j11y.io/?p=572#comment-709 Hey, now that is a really cool use of the DOM API… awesome work.

]]>
By: zhys9 https://j11y.io/snippets/parsing-urls-with-the-dom/#comment-708 Sun, 22 Feb 2009 10:57:40 +0000 https://j11y.io/?p=572#comment-708 cool
reprint url http://zhys9.com/blog/?p=104

]]>