Query string in URL is very useful to work with dynamic content. Generally, server-side language is used to get query string from URL. But you can also get query string parameters from URL to client-side. The query string parameters and values can be easily retrieved from the URL using JavaScript.
The location search property in JavaScript returns the query string part of a URL. The example code shows you how to get query string from URL using JavaScript.
Assume that the URL is http://codexworld.com/index.php?type=product&id=1234
Get Query String Parameters
Use location.search
to get query string parameters including the question mark (?).
var queryString = location.search; // ?type=product&id=1234
Get Query String Parameter Value
The URLSearchParams object is the easiest way to get query string parameter value with JavaScript. The following code shows how to use URLSearchParams to work with the query string of a URL.
var urlParams = new URLSearchParams(location.search); urlParams.has('type'); // true urlParams.get('id'); // 1234 urlParams.getAll('id'); // ["1234"] urlParams.toString(); // type=product&id=1234
var QueryString=”?name=”+subName+”&age=”+subAge+”&phone=”+subPhone+”&email=”+subEmail+”&gender=”+subGender;
window.location=”RegistrationDetails.html”+QueryString;
above method is not navigates to the html page
Wow, just what i was looking for.
May i ask how to put the params on a url after fetching the data?
My goal is to fetch the data after a client uses an email with params. Usually this is used to pre fill a form wich is working fine. But in some cases the client has to go to another site within the website to use another form and then the data is lost.
Could you tell what it should look like if the params arenĀ“t blank and every url gets the data from urlParams.toString()?
This is an example of the paramter: ?klicksub_id=35325369
And this is the url that i need to achive: https://www.mysite.com/?klicksub_id=35325369. In some cases the last slash before ? is missing.
I hope my question is understandable and ok.
Looking forward hearing from you.
Best regards,
Al
Please send your requirements at support@codexworld.com.