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

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

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

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

console.log(url);// http://192.168.1.199/scene.cgi?timestamp=1607496424431

//两个参数时用 & 连接

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

console.log(url2);// http://192.168.1.199/scene.cgi?value=0&timestamp=1607496694517

 

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

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

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

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

 


版权声明:本文为qq_40015157原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。