Hello i want to ask how to copy url parameter to html text
For example my url is:
www.domain.com/?id=123456&name=John
Then i write on html page like this: HTML:
Your ID is <div id="id"></div>
Your Name:
Result:
Your ID is: 123456
Your Name is: John
my question is what the javascript i use to copy url parameter like above to my div html?
Get URL parameters from
const params = new URL('https://www.example.com/id?=123456').searchParams;
Then get the value using
params.get('');
const params = new URL('https://www.example.com/id?=123456').searchParams;
console.log(params.get(''));
document.getElementById('id').innerHTML = params.get('')
// or else
const params2 = new URL('https://www.example.com?id=123456').searchParams;
console.log(params2.get('id'));
Your ID is <div id="id"></div>
you can use this code if you want to get param from window.location.href
let url = new URL(window.location.href).searchParams;
const id = url.get('id');
console.log("Your param id :" + id);
To parse GET parameters from url you can use something like this :
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const id = urlParams.get('id')
console.log(id);
// 123456
const name= urlParams.get('name')
console.log(name);
// John
document.getElementById('id').innerHTML = id;