getUrlParams.js 920 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. 解析URL参数
  3. url:要解析的地址
  4. fkey:要查找的参数
  5. let locationLocaturl = window.location.search;
  6. let openId = getUrlParams(locationLocaturl,"openId");
  7. */
  8. function getUrlParams(url, fkey) {
  9. if (decodeURIComponent(url).indexOf('?') > -1) {
  10. var result = [];
  11. var urlParamsArr = decodeURIComponent(url).split('?');
  12. urlParamsArr.shift();
  13. let newUrlParamsArr = urlParamsArr.join('&').split('&');
  14. for (var i = 0; i < newUrlParamsArr.length; i++) {
  15. var paramKey = newUrlParamsArr[i].split('=')[0];
  16. var paramValue = newUrlParamsArr[i].split('=')[1];
  17. result.push({
  18. key: paramKey,
  19. value: paramValue
  20. });
  21. }
  22. if(fkey){
  23. let keyArr = result.find(function(result) {
  24. return result.key === fkey;
  25. });
  26. if (!!keyArr) {
  27. let fkeyValue = keyArr.value;
  28. return fkeyValue;
  29. }else{
  30. return false;
  31. }
  32. }else{
  33. return result;
  34. }
  35. }
  36. }
  37. export default getUrlParams