Skip to content

Instantly share code, notes, and snippets.

@greypants
Forked from jlong/uri.js
Created May 25, 2012 17:43
Show Gist options
  • Save greypants/2789432 to your computer and use it in GitHub Desktop.
Save greypants/2789432 to your computer and use it in GitHub Desktop.
JS: URI and Query String Parsing
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
var getQueryVariable = function(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
var queryVariable = decodeURIComponent(pair[0]);
var value = decodeURIComponent(pair[1]);
if (queryVariable == variable) {
return value;
}
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment