Last active
March 8, 2017 23:02
-
-
Save webarthur/82b0475b27d851ca675c5e8198610f12 to your computer and use it in GitHub Desktop.
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
// from Ideal.js (https://github.com/webarthur/ideal.js) | |
window.location.getQueryParam = function(name, query) { | |
// se o parâmetro query não foi definido pega o parâmetro da URL | |
if (!query) { | |
query = window.location.search | |
} | |
var l = query.length | |
var n = '' // nome | |
var v = '' // valor | |
var t = false | |
// percorre os caracteres da variavel "query" | |
for (var i=0; i<l; i++) { | |
var c = query[i] | |
// caso o caractere seja = passa a armazenar os caracteres como valor | |
if (c==='=') { | |
t = true | |
} | |
// caso o caractere seja & ou seja o último caractere | |
else if (c==='&' || i===l-1) { | |
// se for o parâmetro desejado, retorna o valor | |
if (n===name) { | |
if (i===l-1) { | |
v += (c === '+') ? ' ' : c | |
} | |
return decodeURIComponent(v) | |
} | |
// senão, reinicia as variáveis de controle | |
t = false | |
n = '' | |
v = '' | |
} | |
// caso a variável não seja igual a interrogação | |
else if (i>0 || c!=='?') { | |
// armazena como valor | |
if (t) { | |
// redefine + como espaço | |
v += (c === '+') ? ' ' : c | |
} | |
// armazena como nome | |
else { | |
n += c | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment