Hent forespørgselsparametre som objekt - CSS-tricks

Anonim

Nicholas Ortenzio skrev dette lille plugin:

jQuery.extend(( getQueryParameters : function(str) ( return (str || document.location.search).replace(/(^\?)/,'').split("&").map(function(n)(return n = n.split("="),this(n(0)) = n(1),this).bind(()))(0); ) ));

Så hvis URL'en er:

http://codepen.io/chriscoyier/pen/uszCr?lunch=sandwich&dinner=stirfry

Du kan gøre:

var queryParams = $.getQueryParameters();

Og queryParamsvil være et objekt som:

( "lunch": "sandwich", "dinner": "stirfry" )