How can I get the query string parameter in Javascript or jQuery?

I have a link like this:

http://localhost:8162/UI/Link2.aspx?txt_temp=123abc

      

I want to get the value 123abc

. I followed this How to get query string values ​​in JavaScript? and jquery get querystring from url

$(document).ready(function () {
    function getUrlVars() {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for (var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
        return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    onload = function () {
        alert(getParameterByName('txt_temp'));
        alert(getUrlVars()["txt_temp"]);
    }  
});

      

But that won't work.

+3


source to share


3 answers


Suppose you have url with many parameters like: -

"http://localhost:8162/UI/Link2.aspx?txt_temp=123abc&a=1&b=2"

      

Then in js you can do like this:

var url = "http://localhost:8162/UI/Link2.aspx?txt_temp=123abc&a=1&b=2"

      

OR

var url = window.location.href

      

then split the main url like:

hashes = url.split("?")[1]

      



// hashes contain this output "txt_temp = 123abc & a = 1 & b = 2"

Then you can split by & to get the individual parameter

EDIT

Check out this example:

function getUrlVars() {
var url = "http://localhost:8162/UI/Link2.aspx?txt_temp=123abc&a=1&b=2";
var vars = {};
var hashes = url.split("?")[1];
var hash = hashes.split('&');

for (var i = 0; i < hash.length; i++) {
params=hash[i].split("=");
vars[params[0]] = params[1];
}
return vars;
}

      

Output

getUrlVars()
Object {txt_temp: "123abc", a: "1", b: "2"}

      

+3


source


This does not work because you are executing functions inside onload

, which does not run inside document.ready

, because by the time the code is executed inside document.ready

, it is onload

already fired. Just enter the code from onload

:

http://jsfiddle.net/whp9hnsk/1/



$(document).ready(function() {

   // Remove this, this is only for testing.
   history.pushState(null, null, '/UI/Link2.aspx?txt_temp=123abc');

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

   function getParameterByName(name) {
       name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
       var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
           results = regex.exec(location.search);
       return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
   }

   // You may also place this inside of a function,
   // and execute it when you desire, but `onload` is not going
   // to fire by itself, when inside of document.ready
   alert(getParameterByName('txt_temp'));
   alert(getUrlVars()["txt_temp"]);

});

      

+3


source


This should help you:

function parseQueryStr( str, obj ) {


    // Return object
    obj = obj || {};


    // Looping through our key/values
    var keyvalues = str.split('&');
    for( var i=0; i<keyvalues.length; i++ ) {


        // Break apart our key/value
        var sides = keyvalues[i].split( '=' );


        // Valid propery name
        if( sides[0] != '' ) {


            // Decoding our components
            sides[0] = decodeURIComponent( sides[0] );
            sides[1] = decodeURIComponent( sides.splice( 1, sides.length-1 ).join( '=' ) );


            // If we have an array to deal with
            if( sides[0].substring( sides[0].length - 2 ) == '[]' ) {
                var arrayName = sides[0].substring( 0, sides[0].length - 2 );
                obj[ arrayName  ] = obj[ arrayName  ] || [];
                obj[ arrayName ].push( sides[1] );
            }


            // Single property (will overwrite)
            else {
                obj[ sides[0] ] = sides[1];
            }
        }
    }


    // Returning the query object
    return obj;
}

var href = window.location.href.split('#');
var query = href[0].split('?');
query.splice(0,1);
var get = parseQueryStr(query.join('?'));

alert( get.txt_temp );

      

+2


source







All Articles