-
-
Save tap52384/ef643e7158f2b670a19e to your computer and use it in GitHub Desktop.
Read URL GET variable (Javascript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given a query string "?to=email&why=because&first=John&Last=smith" | |
// getUrlVar("to") will return "email" | |
// getUrlVar("last") will return "smith" | |
// 'unescape' is now deprecated for security reasons, so it is replaced here by decodeURIComponent | |
// MDN proof: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/unescape | |
// 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 && decodeURIComponent( 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 && decodeURIComponent( result[ 1 ] ) || ''; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment