Last active
December 14, 2017 03:20
-
-
Save Woodsphreaker/82444e2916a31a41793d544cd37f48db to your computer and use it in GitHub Desktop.
Recupera parâmetro de uma querystring
This file contains hidden or 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
const requestQuery = request => { | |
const param = request.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]") | |
const regex = new RegExp("[\\?&]" + param + "=([^&#]*)") | |
const results = regex.exec(location.search); | |
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")) | |
} | |
// caso de uso | |
// http://endereco.com.br?param1=123¶m2=456¶m3=789 | |
console.log(requestQuery('param1')) // 123 | |
console.log(requestQuery('param2')) // 456 | |
console.log(requestQuery('param3')) // 789 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment