• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Search Engine Optimization and Internet Marketing
  • Earning with Website and Blogging
  • Software and Hardware
  • New and Happening
  • Other
  • Sponsored Post
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

  • JavaScript: Overcome Slow Loading JavaScript On A Web PageJavaScript: Overcome Slow Loading JavaScript On A Web Page
  • Adobe Acrobat Reader: Possible Security Vulnerability When Acrobat JavaScript Is EnabledAdobe Acrobat Reader: Possible Security Vulnerability When Acrobat JavaScript Is Enabled
  • Java: Format Integer Into Fixed Width StringJava: Format Integer Into Fixed Width String
  • Java: Format Long Integer Into Hexadecimal StringJava: Format Long Integer Into Hexadecimal String
  • Youtube: Stop Youtube From LoadingYoutube: Stop Youtube From Loading
  • Java: Format integer into number of decimal places and performanceJava: Format integer into number of decimal places and performance

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

Search Engine Optimization

Short URL: Top 5 Websites That Provide Free Short URL Service

December 10, 2009 By Sze Hau Leave a Comment

Google Chromium OS

November 23, 2009 By Sze Hau Leave a Comment

WordPress Plugin: Official StatCounter Plugin

November 13, 2009 By Sze Hau 1 Comment

Make Money With Website And Blogging

Wordpress

WordPress: How To Change Admin Username

September 23, 2015 By Sze Hau Leave a Comment

WordPress: Transfer Your Blogs To Another Hosting With Minimum Downtime

February 3, 2014 By Sze Hau Leave a Comment

Virtualmin: How To Install Virtualmin in CentOS

August 26, 2013 By Sze Hau Leave a Comment

Software and Hardware

iOS 5.1 Unable to Update

March 13, 2012 By Sze Hau Leave a Comment

Blog Network

  • All Gadget Latest Gadget Reviews and News
  • Blog Portal A place where bloggers gather
  • 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
  • Vista Talks Software News, Software Updates, Tips And Tricks

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 © 2021 · Magazine Pro Theme on Genesis Framework · WordPress · Log in