Arens Myzyri Arens Myzyri - 5 months ago 23
HTML Question

How to divide an URL string into parts and insert into variables with jquery

I have an URL:

I'm using this script for taking the URL parameters and inserting them into variables:

var getUrlParameter2 = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(,
sURLVariables = sPageURL.split('&'),
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
var name = getUrlParameter2('myvar');
document.write('<p>URL: '+name+'</p>');

It works when I have something like this:


So going back to the URL above:

I want to have where
the variable and
the value and so go on for all the others.

Please let me know if something is not clear so I can modify the question again. Thanks for your help.


You can do it with following javascript code:

var url = "";

var lstIdxOfSlash = url.lastIndexOf("/");
var paramPart = url.substring(lstIdxOfSlash + 1);
var paramValArray = paramPart.split("+");

var keyValPair = {};

for (var i = 0; i < paramValArray.length; i++) {

  var lstIdxOfDash = paramValArray[i].lastIndexOf("-");
  var key = paramValArray[i].substring(0,lstIdxOfDash);
  var val = paramValArray[i].substring(lstIdxOfDash+1);
  keyValPair[key] = val;