Home > Net >  How to get Url values to javascript variable?
How to get Url values to javascript variable?

Time:11-08

How to get parameter value to javascript variable? example: <a href="page.php?id=10&value='hello'">click me</a>

I want to get Id, and value to js variable? how to do it?

CodePudding user response:

You may use the URLSearchParams class.

const url_params = new URLSearchParams(window.location.search);
const id = url_params.get('id');
console.log(id);

CodePudding user response:

for (let name of document.querySelectorAll("a")) {  
var Reg = /page.php\?id=(\d )\&value='(.*?)'/g;  
var Array;
while ((Array = Reg.exec(name.href)) != null){
console.log(Array[1]);
console.log(Array[2]);
}  
} 

regex

  • Related