Skip to content

Instantly share code, notes, and snippets.

@felipelavinz
Created November 4, 2020 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipelavinz/b0722fef8ffc49e0b1dd03bb6c28039d to your computer and use it in GitHub Desktop.
Save felipelavinz/b0722fef8ffc49e0b1dd03bb6c28039d to your computer and use it in GitHub Desktop.
Parsear parámetros URL iframe
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