js获取地址栏参数方法

需求:

这次需求只写一个页面,只有一个请求接口,但是需要的请求参数是后端通过url拼接传递的。

问题:

取值过程遇到中文乱码问题,已解决;

代码:

getUrlParam(name) {

// eslint-disable-next-line

var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');

var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1);

var r = hashes.substr(0).match(reg);

/**通过decodeURIComponent转码 */

if (r != null) return decodeURIComponent(r[2]);

return null;

}

提示:name是需要取的参数的名字;

函数可以直接使用。只需要传入需要获取的参数的名字。

发布了6 篇原创文章 · 获赞 0 · 访问量 38

猜你喜欢

转载自blog.csdn.net/qq_37585017/article/details/102955521