Thomas
Thomas

Reputation: 34188

Prase query string from url javascript

my url look like http://localhost:13562/Student/RefreshStudents?sort=FirstName&sortdir=ASC&page=1

now i am looking for a function where i will pass url and query string name then that should return value.

so i did it this way but not working.

function getQueryVariable(url,variable) {
    var query = url;
    var vars = query.split('&');
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=');
        if (decodeURIComponent(pair[0]) == variable) {
            return decodeURIComponent(pair[1]);
        }
    }
    console.log('Query variable %s not found', variable);
}

calling like this way

var x='http://localhost:13562/Student/RefreshStudents?sort=FirstName&sortdir=ASC&page=1'

alert(getQueryVariable(x,'sort'));
alert(getQueryVariable(x,'sortdir'));
alert(getQueryVariable(x,'page'));

where i made the mistake?

EDIT

working code

$.urlParam = function(url,name){
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(url);
    if (results==null){
       return null;
    }
    else{
       return results[1] || 0;
    }
}

var x='http://localhost:13562/Student/RefreshStudents?sort=FirstName&sortdir=ASC&page=1'

alert($.urlParam(x,'sort'));
alert($.urlParam(x,'sortdir'));
alert($.urlParam(x,'page'));

https://jsfiddle.net/z99L3985/1/

thanks

Upvotes: 0

Views: 178

Answers (3)

melvnberd
melvnberd

Reputation: 3163

I just get this from somewhere else as well..

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       console.log(vars);
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] === variable){return pair[1];}
       }
       return(false);
}

so far its doing its job. with url: "http://urlhere.com/general_journal?from=01%2F14%2F2016&to=01%2F14%2F2016&per_page=25&page=2"

if im going to get the 'page' variable result would be : `2`
console.log(getQueryVariable('page'));

my query variable is only getting the search.substring(1) part of the the url so basically it only gets from=01%2F14%2F2016&to=01%2F14%2F2016&per_page=25&page=2 part of the url then from that it splits it and then return the value of the string parameter you specified on the function call getQueryVariable('page') for example.

Upvotes: 1

AKHIL K
AKHIL K

Reputation: 94

may be the following will help

   function getUrlVars(url) {
        var vars = {};
        var parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {

            vars[key] = value;
        });
        return vars;
    }

    var x='http://localhost:13562/Student/RefreshStudents?sort=FirstName&sortdir=ASC&page=1';

    var queryVars = getUrlVars(x);
    alert(queryVars['sort']);
    alert(queryVars['sortdir']);
    alert(queryVars['page']);

Upvotes: 1

Harry Bomrah
Harry Bomrah

Reputation: 1668

Maybe this helps

var getUrlVars = function(url){
    var vars = [], hash;
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(decodeURIComponent(hash[0]));
        vars[decodeURIComponent(hash[0])] = decodeURIComponent(hash[1]);
    }
    if(vars[0] == url){
        vars =[];
    }
    return vars;
}

Then in your code

var params = getUrlVars("http://localhost:13562/Student/RefreshStudents?sort=FirstName&sortdir=ASC&page=1");
console.log(params["sort"]) // FirstName

Upvotes: 0

Related Questions