UrlParse.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * @Author: dgflash
  3. * @Date: 2022-07-26 15:27:57
  4. * @LastEditors: dgflash
  5. * @LastEditTime: 2022-08-02 14:26:06
  6. */
  7. import { sys } from "cc";
  8. /* 地址栏参数处理 */
  9. export class UrlParse {
  10. private _data: any = null;
  11. /** URL search参数对象 */
  12. public get query(): any {
  13. return this._data;
  14. }
  15. constructor() {
  16. if (!sys.isBrowser) {
  17. this._data = {};
  18. return;
  19. }
  20. this._data = this.parseUrl();
  21. }
  22. private parseUrl() {
  23. if (typeof window !== "object") {
  24. return {};
  25. }
  26. if (!window.document) {
  27. return {};
  28. }
  29. let url = window.document.location.href.toString();
  30. let u = url.split("?");
  31. if (typeof (u[1]) == "string") {
  32. u = u[1].split("&");
  33. let get: any = {};
  34. for (let i = 0, l = u.length; i < l; ++i) {
  35. let j = u[i].split("=");
  36. get[j[0]] = j[1];
  37. }
  38. return get;
  39. }
  40. else {
  41. return {};
  42. }
  43. }
  44. }