Ahmed Jalal Ahmed Jalal - 1 year ago 108
Javascript Question

how to get Query String value after ? sign in Javascript

I have the following URL:


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"

Answer Source

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
  .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';

var email = emailParam ? emailParam.value : null;


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download