[thelist] Javascript: url variable to form text field
Brothercake
cake at brothercake.com
Thu Mar 13 08:46:16 CST 2003
> I want to use Javascript to grab one particular variable out of a url
> sting containing many variables and I want to populate that variable
> into the text box of a form on the page.
By "url string", I assume you mean a GET string, delimted with & and
=; if so - here's a neat little function which builds a key-indexed
array:
if (location.search){
//*** query string function by Dave Clark -
http://www.daveclarkconsulting.com/ **************
var ary=window.location.search.substr(1).split("&");
for (i=0;i<ary.length;i++) {
ary[i]=ary[i].split("=");
}
var qStr=new Array();
for (i=0;i<ary.length;i++) {
ary[i][0]=ary[i][0].replace(/\+/g," ");
ary[i][0]=unescape(ary[i][0]);
ary[i][1]=ary[i][1].replace(/\+/g," ");
ary[i][1]=unescape(ary[i][1]);
qStr[ary[i][0]]=ary[i][1];
}
}
So a url like "page.html?this=that"
would give you
var thisVar = qStr["this"]; // outputs "that"
More information about the thelist
mailing list