Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6966737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:13:49+00:00 2026-05-27T16:13:49+00:00

Possible Duplicate: Use the get paramater of the url in javascript How can I

  • 0

Possible Duplicate:
Use the get paramater of the url in javascript
How can I get query string values in JavaScript?

In Javascript, how can I get the parameters of a URL string (not the current URL)?

like:

www.domain.com/?v=123&p=hello

Can I get “v” and “p” in a JSON object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:13:49+00:00Added an answer on May 27, 2026 at 4:13 pm

    2.5 years after the question was asked you can safely use Array.forEach. As @ricosrealm suggests, decodeURIComponent was used in this function.

    function getJsonFromUrl(url) {
      if(!url) url = location.search;
      var query = url.substr(1);
      var result = {};
      query.split("&").forEach(function(part) {
        var item = part.split("=");
        result[item[0]] = decodeURIComponent(item[1]);
      });
      return result;
    }
    

    actually it’s not that simple, see the peer-review in the comments, especially:

    • hash based routing (@cmfolio)
    • array parameters (@user2368055)
    • proper use of decodeURIComponent and non-encoded = (@AndrewF)
    • non-encoded + (added by me)

    For further details, see MDN article and RFC 3986.

    Maybe this should go to codereview SE, but here is safer and regexp-free code:

    function getSearchOrHashBased(url) {
      if(!url) url = location.href;
      var question = url.indexOf("?");
      var hash = url.indexOf("#");
      if(hash==-1 && question==-1) return {};
      if(hash==-1) hash = url.length;
      return question==-1 || hash==question+1
        ? url.substring(hash)
        : url.substring(question+1, hash);
    }
    
    // use query = getSearchOrHashBased(location.href)
    // or query = location.search.substring(1)
    function getJsonFromUrl(query) {
      var result = {};
      query.split("&").forEach(function(part) {
        if(!part) return;
        part = part.split("+").join(" "); // + to space, regexp-free version
        var eq = part.indexOf("=");
        var key = eq>-1 ? part.substr(0,eq) : part;
        var val = eq>-1 ? decodeURIComponent(part.substr(eq+1)) : "";
        var from = key.indexOf("[");
        if(from==-1) result[decodeURIComponent(key)] = val;
        else {
          var to = key.indexOf("]",from);
          var index = decodeURIComponent(key.substring(from+1,to));
          key = decodeURIComponent(key.substring(0,from));
          if(!result[key]) result[key] = [];
          if(!index) result[key].push(val);
          else result[key][index] = val;
        }
      });
      return result;
    }
    

    This function can parse even URLs like

    var url = "?foo%20e[]=a%20a&foo+e[%5Bx%5D]=b&foo e[]=c";
    // {"foo e": ["a a",  "c",  "[x]":"b"]}
    
    var obj = getJsonFromUrl(url)["foo e"];
    for(var key in obj) { // Array.forEach would skip string keys here
      console.log(key,":",obj[key]);
    }
    /*
      0 : a a
      1 : c
      [x] : b
    */
    

    7 years after the question was asked the functionality was standardized as URLSearchParams and 4 more years after, the access can be further simplified by Proxy as explained in this answer, however that new one can not parse the sample url above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: getting current URL I wan't to use url (with any GET parameters,
Possible Duplicate: Escape string for use in Javascript regex I have a msg like
Possible Duplicate: How do I use PHP to get the current year? 2009 this
Possible Duplicate: Use templates to get an array's size and end address Can someone
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: Can I use ASP.NET MVC together with regular ASP.NET Web forms Let's
Possible Duplicate: PHP class instantiation. To use or not to use the parenthesis? Omission
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic This
Possible Duplicate: Deflate command line tool Yes, I know I can use PHP itself

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.