parseQueryString.js 470 B

12345678910111213141516
  1. /**
  2. *
  3. * @desc url参数转对象
  4. * @param {String} url default: window.location.href
  5. * @return {Object}
  6. */
  7. function parseQueryString(url) {
  8. url = url == null ? window.location.href : url;
  9. var search = url.substring(url.lastIndexOf('?') + 1);
  10. if (!search) {
  11. return {}
  12. }
  13. return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}');
  14. };
  15. module.exports = parseQueryString;