Created
November 4, 2020 19:14
-
-
Save felipelavinz/b0722fef8ffc49e0b1dd03bb6c28039d to your computer and use it in GitHub Desktop.
Parsear parámetros URL iframe
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
var parametrosIframe = 'programas[0][ca]=CODIGO_ACADEMICO&programas[0][nombre]=Nombre+del+programa&programas[0][planificacion]=CODIGO_PLANIFICACION&programas[1][ca]=CODIGO_ACADEMICO_2&programas[1][nombre]=Nombre+programa+2&programas[1][planificacion]=CODIGO_PLANIFICACION_2'; | |
var infoProgramas = []; | |
var urlParams = new URLSearchParams( parametrosIframe ); | |
var regex = /programas\[(\d+)\]\[(\w+)\]/; | |
for ( var i of urlParams ) { | |
var matches = i[0].match(regex); | |
if ( typeof infoProgramas[ matches[ 1 ] ] === 'undefined' ) { | |
infoProgramas[ matches[ 1 ] ] = {}; | |
} | |
infoProgramas[ matches[1] ][ matches[ 2 ] ] = decodeURIComponent( i[1] ); | |
} | |
console.log( infoProgramas ); | |
// [ | |
// { | |
// ca: 'CODIGO_ACADEMICO', | |
// nombre: 'Nombre del programa', | |
// planificacion: 'CODIGO_PLANIFICACION' | |
// }, | |
// { | |
// ca: 'CODIGO_ACADEMICO_2', | |
// nombre: 'Nombre programa 2', | |
// planificacion: 'CODIGO_PLANIFICACION_2' | |
// } | |
// ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment