Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  26] [ 6]  / answers: 1 / hits: 32489  / 9 Years ago, sat, may 16, 2015, 12:00:00

I have a link like this:



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


I want to get the value 123abc . I have followed this How can I 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 it does not work.


More From » jquery

 Answers
3

Suppose you have URL with many params eg:-



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


Then in js you can do like:



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


OR



var url = window.location.href


then split main url like:



hashes = url.split(?)[1]


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



Then again you can split by & to get individual param



EDIT



Check 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}

[#66587] Wednesday, May 13, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronniem

Total Points: 584
Total Questions: 111
Total Answers: 111

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
ronniem questions
Sat, Apr 24, 21, 00:00, 3 Years ago
Wed, Mar 10, 21, 00:00, 3 Years ago
Sat, Feb 6, 21, 00:00, 3 Years ago
;