Split Url Into Tab In Javascript
input: '/desh/HRTY/THR/TDR/2015-01-09?passengers=STANDARD:1&returnDate=2015-01-10&max=0&withThac=false' javascript: var params = {}; var paramDelim = link.indexOf('?')
Solution 1:
You should split with
varparams = parmeters.split('&')
and then split all the values you get
for (var i = 0,len = params.length; i<len;i++){
vardata = params[i].split("=", 2); // Max 2 elementsvar key = data[0];
var value = data[1];
...
}
Solution 2:
i think your wrong ' characters
var params = {};
var paramDelim = link.indexOf('?');
var parmeters = link.substring(paramDelim + 1, link.length);
/*--> i think used regexp. Clear ' Char. --> */var parts = parmeters.split(/[&=]/);
use this like..
good luck
Solution 3:
A possible solution using ECMA5 methods and assuming that your string is always the same pattern.
var src = '/desh/HRTY/THR/TDR/2015-01-09?passengers=STANDARD:1&returnDate=2015-01-10&max=0&withThac=false',
slice = src.split(/[\/|?|&]/).slice(3),
data = slice.reduce(function (output, item) {
var split = item.split('=');
output[split.shift()] = split.shift();
return output;
}, {
origin: slice.shift(),
destination: slice.shift(),
goDate: slice.shift()
});
document.body.appendChild(document.createTextNode(JSON.stringify(data)));
Post a Comment for "Split Url Into Tab In Javascript"