Created
June 4, 2014 07:06
-
-
Save fernandojunior/857c9926ad4de671bc0e to your computer and use it in GitHub Desktop.
Funções JavaScript que recuperam parametros da url
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
/** | |
* Retorna o valor de um parametro da url | |
* @param name Nome do parametro | |
* @ref http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript | |
**/ | |
function getParameter(name) { | |
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); | |
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), | |
results = regex.exec(location.search); | |
return results === null ? null : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
/** | |
* Retorna todos os parametros da url em um dicionario | |
* @ref http://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript | |
**/ | |
function getParameters() { | |
var transformToAssocArray = function(prmstr) { | |
var params = {}; | |
var prmarr = prmstr.split("&"); | |
for ( var i = 0; i < prmarr.length; i++) { | |
var tmparr = prmarr[i].split("="); | |
params[tmparr[0]] = tmparr[1]; | |
} | |
return params; | |
} | |
var prmstr = window.location.search.substr(1); | |
return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment