I was searching and found nothing , I have a page specifications.html which has three link like details.html?id=1 and details.html?id=2 and details.html?id=3 , I want to show the details of each link if someone click on details.html?id=1 link on specifications.html page then it show id =1 on details.html page , I can do it with php but my teacher said me to do it with javascript only and I dont know how to do it , so please if anyone help me out I will be highly thankfull to you
Asked
Active
Viewed 30 times
-1
-
You want to know what is the "id" value in the page you clicked for? – E. Zacarias Nov 08 '18 at 13:17
-
there is link on page one page2.html?id=1 if i click on it it goes to page 2 with id=1 and i want to write this id in a javascript variable in page 2 , like var id = 1 – rahul Nov 08 '18 at 13:28
-
Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – E. Zacarias Nov 08 '18 at 13:30
1 Answers
1
As far as I get it, you want to know the value of the "id" search param when accessing the "details.html" page. As stated in this answer to the question "How can I get query string values in JavaScript?", you can use URLSearchParams which is simple and has good browser support.
const urlParams = new URLSearchParams(window.location.search);
const id = urlParams.get('id'); // for a search "&id=2", the value of "id" will be 2.
This will create an URLSearchParams object which parses the window.location.search
string and is able to return one specific param, like "id".

E. Zacarias
- 598
- 6
- 19