No Edge Software
  • Search results
No Edge Software

Read query string with URI.js

Description

It’s kind of a hassle to pull query string data using javascript. Here’s how to pull key-value pairs from the query string into your javascript.

Sample Code

// <script src="https://bowercdn.net/c/urijs-1.18.10/src/URI.min.js"></script>

// Page is: http://example.com/index.html?a=1&b=2

var parsedURI = URI.parse(document.location.href);
// { query: "a=1&b2=", protocol: "http", hostname: "example.com", ... }

var query = parsedURI.query;
// a=1&b=2

var parsedQuery = URI.parseQuery(query);
// { a: "1", b: "2" }

var a = parsedQuery.a;
// "1"

// In a single line:
a = URI.parseQuery(URI.parse(document.location.href).query).a;
// "1"

Author

Joe Enos