I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
// I'd like to see a minifier work that hard.document.location.search.replace(/(^\?)/,'').split('&').reduce(function(o,n){n=n.split('=');o[n[0]]=n[1];returno},{});// Challenge accepted.location.search.substr(1).split('&').reduce((o,n)=>{n=n.split('=');o[n[0]]=n[1];returno},{});
I'm not a big Javascript person so I don't know what the difference is between window.location, document.location and just location (which I assume uses the global window context). I know substr works fine on an empty string, and I'm making the (heroically cavalier) assumption that query strings start with a ? in all browsers' implementations of location.search.
But both of these return something incorrect if there's no query string:
I'm not a big Javascript person so I don't know what the difference is between
window.location
,document.location
and justlocation
(which I assume uses the global window context). I knowsubstr
works fine on an empty string, and I'm making the (heroically cavalier) assumption that query strings start with a?
in all browsers' implementations oflocation.search
.But both of these return something incorrect if there's no query string:
Oops. Well, we can do something about that:
And now it's longer and even less maintainable, but hey :)
This is brilliant, Ben! 😄
I can probably do it in even less characters... let me try:
Hey, that one could not accept empty value (eg:
'?a=&b=1&c='
)try this instead: