-
-
Save jed/964849 to your computer and use it in GitHub Desktop.
use anchor tags to parse URLs into components
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
a // an anchor element | |
){ | |
return function( // return a function | |
b, // that takes a URL. | |
c, // (placeholder) | |
d // (placeholder) | |
){ | |
a.href = b; // set the target of the link to the URL, and | |
c = {}; // create a hash to be returned. | |
for ( // for each | |
d // property | |
in a // of the anchor, | |
) if ( // if | |
"" + a[d] // the string version of the property | |
=== a[d] // is the same as the property | |
) c[d] // add it | |
= a[d]; // to the return object | |
return c // return the object | |
} | |
}( | |
document // auto-run with | |
.createElement // a cached anchor element | |
("a") | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a){return function(b,c,d){a.href=b;c={};for(d in a)if(""+a[d]===a[d])c[d]=a[d];return c}}(document.createElement("a")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Jed Schmidt <http://jed.is> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "parseURL", | |
"keywords": ["parse", "URL", "link"] | |
} |
changed, thanks!
but would love to have a better solution to this, so that i could only capture the url components and not everything else...
Parsing URL such method is not only parsing!
Browsers resolves urls relative to document, so this algorithm is "resolve+parse"
Not sure if this existed back then but URL
?
new URL('https://google.com')
// => URL {hash: "", host: "google.com", hostname: "google.com", href: "https://google.com/", origin: "https://google.com", password: "", pathname: "/", port: "", protocol: "https:", search: "", searchParams: URLSearchParams {}, username: "", [[Prototype]]: URL}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about rewriting
typeof a[d]=="string"
as""+a[d]===a[d]
?Whoops, looks like that would throw
Uncaught TypeError: Cannot convert object to primitive value
for thedataset
property in Chrome. Seems to work fine in Opera and Firefox though. Chrome bug: http://code.google.com/p/chromium/issues/detail?id=59994