You must Sign In to post a response.
  • Category: JQuery

    Extract values from url using jquery

    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
  • #767012
    Hi,
    Call this javascript function on button click:

    function getUrlParams ()
    {
    var szPageURL = decodeURIComponent(window.location.search.substring(1)),
    var sURLVariables = szPageURL.split('&');
    var sParameterName;
    var i;

    for (i = 0; i < sURLVariables.length; i++)
    {
    if(sParameterName == "")
    sParameterName = sURLVariables[i];
    else
    sParameterName = sParameterName + ", " + sURLVariables[i];
    }
    alert(sParameterName);
    }

  • #767014
    Once you get the URL, you can extract the query string. If you get the query string values, you can use split functionality on that. Split function will give you the string array. From the string array you can get your required values.

    var MyVariables = YouQueryStringParameter.split(',');

    In the above case MyVariables will be the string array. you can get the values by using the index MyVariables[0], MyVariables [2], etc...

    By Nathan
    Direction is important than speed

  • #767084
    There is no direct way to fetch query string values using JQuery, you need to use brower URL and split them to get different values
    see below snippet

    //my URL is
    "WebForm2.aspx?Name="+txtName.Text+"&ID="+txtID.Text

    //now in JQuery I can read it and get Name and ID as follows
    < script>
    $(document).ready(function () {
    var name = GetParameterValues('Name');
    var id = GetParameterValues('ID');
    alert("Hello " + name + " your ID is " + id);
    function GetParameterValues(param) {
    var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < url.length; i++) {
    var urlparam = url[i].split('=');
    if (urlparam[0] == param) {
    return urlparam[1];
    }
    }
    }
    });
    < /script>

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments