JavaScript获取URL中的参数

2021-05-21 17:35 阅读:1966

使用以下JS函数用于获取url参数:

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split('&');
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split('=');
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return false;
}

示例:

如果URL为https:www.regaing.cn/lingyun?code=123456789 

let code = getQueryVariable('code');
console.log(code);

则code输出123456789 

{{commentTotal}} 条评论

{{item.nickname}}
{{item.create_date}}
{{item.content}}
- 上拉或点击加载更多 -
- 加载中 -
- 没有更多了 -
- 本文链接 -