Extract and arrange value of url using jquery [Resolved]

Posted by Klbaiju under jQuery on 7/1/2016 | Points: 10 | Views : 487 | Status : [Member] | Replies : 2
Hi following is part of a url

LekshmiTravels/Places.aspx?fplace=Mumbai%20Central,%20 Mumbai,%20 Maharashtra,%20India&tplace=Vikroli%20Station%20(East),%20Vikhroli%20East,%20Mumbai, %20Maharashtra,%20India,&t1=xyz&dt2=klty

my requirement is to extract values from this url

here fplace=Mumbai%20Central,%20 Mumbai,%20 Maharashtra,%20India

there are 3 comma separator in fplace .i need the last 2 comma separator values means avoid mumbai central

and fplace should be Mumbai,Maharashtra,India

and tplace also should last 2 commaseparate values

ie Mumbai,Maharashtra,India and

t1=xyz and t2=klty

How it is possible using jquery

Regards

Baiju




Responses

Posted by: A2H on: 7/3/2016 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
You are trying to read querystring values from url. You can use the below javascript function to read the querystring values from url.

function getParameterByName(name) {


var url = window.location.href;
//Sample string
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}

You can use above function to read values like below
//Get t1 value

var t1value = getParameterByName('t1');

//Get t2 value
var t2value= getParameterByName('dt2');

var array1 = getParameterByName('fplace').split(",");
var fplacevalue = array1.slice(Math.max(array1.length - 5, 1)).join(",");

Complete Sample Code

<script src="https://code.jquery.com/jquery-1.11.3.js"></script>

<script>
$(document).ready(function() {
//Get t1 value
var t1value = getParameterByName('t1');
alert(t1value);
//Get t2 value
var t2value= getParameterByName('dt2');
alert(t2value);
var array1 = getParameterByName('fplace').split(",");
var fplacevalue = array1.slice(Math.max(array1.length - 5, 1)).join(",");
alert(fplacevalue);
});



function getParameterByName(name) {
//Uncomment this to get the url of page
//var url = window.location.href;
//Sample string
var url = "LekshmiTravels/Places.aspx?fplace=Mumbai%20Central,%20 Mumbai,%20Maharashtra,%20India&tplace=Vikroli%20Station%20(East),%20Vikhroli%20East,%20Mumbai, %20Maharashtra,%20India&t1=xyz&dt2=klty";
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
</script>



Thanks,
A2H
My Blog

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 7/3/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can find a demo here :https://jsfiddle.net/arqv4wab/

Thanks,
A2H
My Blog

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response