js 地址栏url 拼接一个或多个参数传参和获取参数方法

时间:2025-04-07 11:14:54

1、js 地址栏url 传一个参数和多个参数时拼接方法:

let ipaddr = "192.168.1.199";
let path = "/";
//以上都可以动态设置当参数传进去

//一个参数
let getTimestamp = new Date().getTime();//时间戳
let url= "http://" + ipaddr + path + "?timestamp=" + getTimestamp;

(url);// http://192.168.1.199/?timestamp=1607496424431

//两个参数时用 & 连接

let v = 0;
let url2 = "http://" + ipaddr + path + "?value=" + v + "&timestamp=" + getTimestamp;

(url2);// http://192.168.1.199/?value=0&timestamp=1607496694517

 

2、js获取地址栏参数方法:

http://localhost:63327/demo/?t=参数&m=1 //地址栏

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = (1).match(reg);
    if(r!=null)return  decodeURIComponent(r[2]); return null;
}

// 调用方法
(GetQueryString("t"));// 参数
(GetQueryString("m"));// 1