0

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

4
  • What is the output of getQueryVariable(x,'sort')? (i.e. what is the wrong) Commented Jan 14, 2016 at 9:47
  • 1
    Maybe duplicate of stackoverflow.com/questions/19491336/get-url-parameter-jquery Commented Jan 14, 2016 at 9:49
  • Also like this plugin: github.com/websanova/js-url Commented Jan 14, 2016 at 9:52
  • i use that code but not working. see js fiddle where i test the code jsfiddle.net/z99L3985 what is the mistake? Commented Jan 14, 2016 at 9:55

3 Answers 3

1

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']);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.