Creating a new Location object in javascript

后端 未结 4 1519
夕颜
夕颜 2020-11-27 12:50

Is it possible to create a new Location object in javascript? I have a url as a string and I would like to leverage what javascript already provides to gain access to the di

4条回答
  •  粉色の甜心
    2020-11-27 13:37

    You can parse it in a regex to get the parts as matches... I don't have the full code right now, but this can be used to get the querydata:

    var myUrl = window.location.href;
    var matches = myUrl.match(/([^\?]+)\?(.+)/);
    var queryData = matches[2];
    

    matches[0] is the full string, matches(1) is the first part of the URL (up to the ?)... you could build up a regular expression to parse each part of a string url if you want...

    You can also use one of the many libraries already out there for this.

提交回复
热议问题