Skip to content

Instantly share code, notes, and snippets.

@toddheslin
Created November 23, 2014 19:44
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 toddheslin/0748c5fc0b07164a8b1a to your computer and use it in GitHub Desktop.
Save toddheslin/0748c5fc0b07164a8b1a to your computer and use it in GitHub Desktop.
Setting url params as cookies for wufoo forms
// Requires cookies.js to be loaded globally, or added as part of the Wufoo script. Added here as part of script for convinence.
<script>
/*\
|*|
|*| :: cookies.js ::
|*|
|*| A complete cookies reader/writer framework with full unicode support.
|*|
|*| Revision #1 - September 4, 2014
|*|
|*| https://developer.mozilla.org/en-US/docs/Web/API/document.cookie
|*|
|*| This framework is released under the GNU Public License, version 3 or later.
|*| http://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*| Syntaxes:
|*|
|*| * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|*| * docCookies.getItem(name)
|*| * docCookies.removeItem(name[, path[, domain]])
|*| * docCookies.hasItem(name)
|*| * docCookies.keys()
|*|
\*/
var docCookies = {
getItem: function (sKey) {
if (!sKey) { return null; }
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
case Number:
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
break;
case String:
sExpires = "; expires=" + vEnd;
break;
case Date:
sExpires = "; expires=" + vEnd.toUTCString();
break;
}
}
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
return true;
},
removeItem: function (sKey, sPath, sDomain) {
if (!this.hasItem(sKey)) { return false; }
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "");
return true;
},
hasItem: function (sKey) {
if (!sKey) { return false; }
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
keys: function () {
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var nLen = aKeys.length, nIdx = 0; nIdx < nLen; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
return aKeys;
}
};
// The following code is based on a contribution by @dorward on stackoverflow. It's been modified to set the cookies from cookies.js
// Like the Wufoo embed, it doesn't rely on jQuery. Thanks @dorward!
var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = pair[1];
docCookies.setItem(pair[0],pair[1],Infinity,document.location.host,null);
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]], pair[1] ];
query_string[pair[0]] = arr;
docCookies.setItem(pair[0],pair[1],Infinity,document.location.host,null)
// If third or later entry with this name
} else {
query_string[pair[0]].push(pair[1]);
docCookies.setItem(pair[0],pair[1],Infinity,document.location.host,null)
}
}
return query_string;
}();
// for this part down, best to copy the script from your wufoo admin panel however pay close attention to the if statement below and the defaultValues in the first 'if code block'
var z7zupww0vb98kb;
(function(d, t) {
// ensure you update 'first' and 'last' with the variables you are looking for in the URL. Change this expression if you require more variables to be present before you load the default Values in
if (docCookies.hasItem('first') && docCookies.hasItem('last')){
var s = d.createElement(t), options = {
'userName':'######', //this is your username for Wufoo
'formHash':'######', //this is your form hash from wufoo
'autoResize':true,
'height':'540',
'async':true,
'host':'wufoo.com',
'header':'hide',
'ssl':true,
//replace 'first' and 'last' with the names of the params within your url
'defaultValues':'field1='+docCookies.getItem('first')+'&field2='+docCookies.getItem('last')};
} else {
var s = d.createElement(t), options = {
'userName':'#####', // same as above
'formHash':'#####', // same as above
'autoResize':true,
'height':'540',
'async':true,
'host':'wufoo.com',
'header':'hide',
'ssl':true};
};
s.src = ('https:' == d.location.protocol ? 'https://' : 'http://') + 'www.wufoo.com/scripts/embed/form.js';
s.onload = s.onreadystatechange = function() {
var rs = this.readyState; if (rs) if (rs != 'complete') if (rs != 'loaded') return;
try { z7zupww0vb98kb = new WufooForm();z7zupww0vb98kb.initialize(options);z7zupww0vb98kb.display(); } catch (e) {}};
var scr = d.getElementsByTagName(t)[0], par = scr.parentNode; par.insertBefore(s, scr);
})(document, 'script');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment