如何从源页面检索查询字符串?

时间:2018-06-25 23:38:41

标签: arrays typescript split substring angular6

我想使用源页面位置中的查询字符串。 以下是我的解决方案: 目标网址是“ http://www.example.com/index.html?ContextId=1234&userId=1&name=dummy#/

this.temploc = location.href;
this.loc = decodeURIComponent(this.temploc);
this.arrLoc = this.loc.split('?');
this.params = this.arrLoc[1].split('&');
this.arrcontextid = this.params[0].split("=");
this.arruserId = this.params[1].split("=");
this.arrbssName = this.params[2].split("=");
this.contextid = this.arrcontextid[1];
this.userId = this.arruserId[1];
this.name= this.arrbssName[1].substring(0, this.arrbssName[1].length - 2);

使用上述逻辑,我能够获取contextId,userId和name的正确值。是否有优化的解决方案/方法来获得这些值?

0 个答案:

没有答案