Skip to content

Instantly share code, notes, and snippets.

@yy-dev7
Created January 9, 2018 06:18
Show Gist options
  • Save yy-dev7/8d143a4a31bcbc49be44204135be3eeb to your computer and use it in GitHub Desktop.
Save yy-dev7/8d143a4a31bcbc49be44204135be3eeb to your computer and use it in GitHub Desktop.
获取URL参数
/**
* 获取URL参数
* // http://jsfiddle.net/draft/?foo=foo&bar=bar
console.log(getUrlParams('foo')); // "foo"
console.log(getUrlParams()); // {foo: "foo", bar: "bar"}
*/
function getUrlParams(name, uri) {
const params = {}
const search = uri || window.location.search
search.replace(/[?&]+([^=&]+)=([^&]*)/gi, (str, key, value) => {
params[key] = value
})
return params[name] || params
}
export default getUrlParams
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment