Reputation: 18660
I need to get some parameters from URL using Javascript/jQuery and I found this nice function:
function getURLParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam) {
return sParameterName[1];
}
}
};
So I started to use in my code but I'm having some issues since the parameter I'm looking for comes undefined
. First, this is a Symfony2 project so Profiler
gives me this information:
Request Attributes
Key Value
_route_params [registro => 1, solicitud => 58]
...
registro 1
solicitud 58
What I need from here is registro
and solicitud
. So what I did at Javascript side was this:
console.log(getURLParameter('registro')); // gets undefined
But surprise I get undefined
and I think the cause is that registro
is not present at URL which is http://project.dev/app_dev.php/proceso/1/58/modificar
but it's present in the REQUEST. How I can get the registro
and solicitud
values from whithin Javascript? I need to send those parameters to a Ajax call, any advice?
Upvotes: 1
Views: 1347
Reputation: 4578
Try using this function:
function getParameters(){
var url = window.location.href; //get the current url
var urlSegment = url.substr(url.indexOf('proceso/')); //copy the last part
var params = urlSegment.split('/'); //get an array
return {registro: params[1], solicitud: params[2]}; //return an object with registro and solicitud
}
Then you can call the function and use the values:
var params = getParameters();
console.log(params.registro);
console.log(params.solicitud);
Upvotes: 1