I have the following URL:
https://localhost:44300/products/5746ba28804136004d060002?email=abc@gmail.com
What I need is to get the value of email from the URL into a variable and then use it in a jQuery code:
var email= "getthequerystringhere"
I have the following URL:
https://localhost:44300/products/5746ba28804136004d060002?email=abc@gmail.com
What I need is to get the value of email from the URL into a variable and then use it in a jQuery code:
var email= "getthequerystringhere"
window.location.search
contains any query string in the current page's URL. You can strip off the ?
at the beginning to get the part you want:
var queryString = window.location.search.replace(/^\?/, '');
If you already have the URL in a string, you can split it on the ?
:
var queryString = url.split('?', 2)[1] || '';
Once you have the query string, you can split it on &
and then on =
to get the particular parameter you're looking for:
var url = 'https://localhost:44300/products/5746ba28804136004d060002?email=abc@gmail.com';
var queryString = url.split('?', 2)[1] || '';
// split query string into key/value pairs
var parameters = queryString
.split('&')
.map(function(param) {
var pair = param.split('=', 2);
return {
name: decodeURIComponent(pair[0]),
value: decodeURIComponent(pair[1] || '')
};
});
// get the parameter with name "email" if it exists
var emailParam = parameters.filter(function(param) {
return param.name === 'email';
})[0];
var email = emailParam ? emailParam.value : null;
console.log(email);
You can do it like this:
var url = "https://localhost:44300/products/5746ba28804136004d060002?email=abc@gmail.com";
var email = url.split("?email=")[1];