Skip to content

Instantly share code, notes, and snippets.

@bxmas13
Forked from varemenos/getparam.js
Last active February 14, 2017 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bxmas13/dda3364b5db62ff06381e229f97ebba7 to your computer and use it in GitHub Desktop.
Save bxmas13/dda3364b5db62ff06381e229f97ebba7 to your computer and use it in GitHub Desktop.
JQuery - GET URL Parameter value
// Given a query string "?to=email&why=because&first=John&Last=smith"
// getUrlVar("to") will return "email"
// getUrlVar("last") will return "smith"
// Slightly more concise and improved version based on http://www.jquery4u.com/snippets/url-parameters-jquery/
function getUrlVar(key){
var result = new RegExp(key + "=([^&]*)", "i").exec(window.location.search);
return result && decodeURI(result[1]) || "";
}
// To convert it to a jQuery plug-in, you could try something like this:
(function($){
$.getUrlVar = function(key){
var result = new RegExp(key + "=([^&]*)", "i").exec(window.location.search);
return result && decodeURI(result[1]) || "";
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment