Skip to content

Instantly share code, notes, and snippets.

@chernomyrdin
Created October 27, 2011 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chernomyrdin/1319414 to your computer and use it in GitHub Desktop.
Save chernomyrdin/1319414 to your computer and use it in GitHub Desktop.
Parse query_string and fill form
function pqsaff () {
this.vars = (window.location.search.length > 1) ?
window.location.search.substring(1).split("&") :
[];
}
pqsaff.prototype = {
param: function (s) {
var p = s.split("=");
this.name = p.shift();
this.value = unescape(p.join("="));
},
fill: function (name,xlate) {
var f = document.forms[name];
if (f && this.vars.length) {
for (var i = 0; i < this.vars.length; i++) {
var v = new this.param( this.vars[i] );
var field = f.elements[xlate ? xlate[v.name] : v.name];
if (field) {
field.value = v.value;
}
}
}
return f;
}
};
/*
* Usage:
*
* (new pqsaff()).fill("theFormName");
* (new pqsaff()).fill("theFormName",{email:"mail",firstName:"fn",lastName:"ln"});
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment