Sogrey / Web-QA

https://sogrey.github.io/Web-QA/
MIT License
6 stars 2 forks source link

js 获取URL参数 #344

Open Sogrey opened 3 years ago

Sogrey commented 3 years ago
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg); //search,查询?后面的参数,并匹配正则
    if (r != null) return decodeURI(r[2]);
    return null;
}

使用举例:

// 比如当前网址: 'http://sogrey.top/Linux-start/zh/commands/?type=FileSystem'
GetQueryString('type'); //FileSystem