• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / Javascript: Get Query String and Key-Value Pairs from URL

Javascript: Get Query String and Key-Value Pairs from URL

November 14, 2012 by Sze Hau Leave a Comment

In web development, it is important to know how to get query string at the client side from the URL being sent to the server. It is useful when your code (Javascript) that run at client’s browser need to know what data has been sent to the server and use the information to produce further more useful information.

To get the query string from URL is pretty simple but to get the key-value pair data is a bit more tricky. To get the full query string from URL (the URL shown on the browser’s location), you can use the following window object in Javascript:

var queryString = "";
if (window.location.search.length > 1)
    queryString = window.location.search.substring (1);

From the queryString value, you then need to split the string into key-value pairs using “&”.

var elements = queryString.split ("&");

The elements current will contain key, following by = and then follow by encoded value. For each of the splited element, we need to get it key and decode the encoded value put it into an array map.

var keyValues = {};
for(var i in elements) { 
    var key = elements[i].split("=");
    if (key.length > 1) {
      keyValues[decodeURIComponent(key[0].replace(/\+/g, " "))] = decodeURIComponent(key[1].replace(/\+/g, " "));
    }
}

And then you can use the keyValues to get the value you need in your application. For example

var value = keyValues["hello"];

More from my site

  • Java: Format Integer Into Fixed Width StringJava: Format Integer Into Fixed Width String
  • Adobe Acrobat Reader: Possible Security Vulnerability When Acrobat JavaScript Is EnabledAdobe Acrobat Reader: Possible Security Vulnerability When Acrobat JavaScript Is Enabled
  • JavaScript: Overcome Slow Loading JavaScript On A Web PageJavaScript: Overcome Slow Loading JavaScript On A Web Page
  • Java: Format Long Integer Into Hexadecimal StringJava: Format Long Integer Into Hexadecimal String
  • Java: Format integer into number of decimal places and performanceJava: Format integer into number of decimal places and performance
  • SEO: Get Higher Ranking in Search EnginesSEO: Get Higher Ranking in Search Engines

Filed Under: Programming and Scripting, Tips and Tricks Tagged With: JavaScript, Key-Value, query, String, URL

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in