Last active
March 8, 2017 23:01
-
-
Save webarthur/31bbdf278b0cc05650b077af3e85905a to your computer and use it in GitHub Desktop.
Ideal.js
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.getQueryParams = function(query) { | |
// se o parâmetro query não foi definido pega o parâmetro da URL | |
if (!query) { | |
query = window.location.search.substring(1) | |
} | |
var l = query.length | |
var q = {} // objeto a ser retornado | |
var n = '' // nome | |
var v = '' // valor | |
var t = false | |
// percorre os caracteres da variavel "query" | |
for (var i=0; i<l; i++) { | |
// pega o caractere | |
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) { | |
if (i===l-1) { | |
v += (c === '+') ? ' ' : c | |
} | |
// armazena no objeto de retorno | |
q[n] = decodeURIComponent(v) | |
// reinicia variáveis | |
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 caractere "+" como espaço | |
v += (c === '+') ? ' ' : c | |
} | |
// armazena como nome | |
else { | |
n += c | |
} | |
} | |
} | |
// retorna objeto | |
return q | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment