Skip to content

Instantly share code, notes, and snippets.

@toddheslin
Last active December 22, 2016 15:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toddheslin/3b8b51d3d48e4568af09 to your computer and use it in GitHub Desktop.
Save toddheslin/3b8b51d3d48e4568af09 to your computer and use it in GitHub Desktop.
Setting url params as variables in Wufoo embed form
/*
// The following code is from @dorward answering a stackoverflow question.
// Like the Wufoo embed, it doesn't rely on jQuery. Thanks @dorward!
*/
<div id="wufoo-m2oq9xw0yusu89"></div>
<script type="text/javascript">
var QueryString = function () {
/*
This function is anonymous, is executed immediately and
the return value is assigned to the variable '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];
// 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;
// If third or later entry with this name
} else {
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
} ();
/*
For this part down, it's 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 m2oq9xw0yusu89;(function(d, t) {
if (window.location.search === ""){
var s = d.createElement(t), options = {
'userName':'beingremarkable',
'formHash':'m2oq9xw0yusu89',
'autoResize':true,
'height':'828',
'async':true,
'host':'wufoo.com',
'header':'hide',
'ssl':true};
s.src = ('https:' == d.location.protocol ? 'https://' : 'http://') + '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 { m2oq9xw0yusu89 = new WufooForm();m2oq9xw0yusu89.initialize(options);m2oq9xw0yusu89.display(); } catch (e) {}};
var scr = d.getElementsByTagName(t)[0], par = scr.parentNode; par.insertBefore(s, scr);
} else {
var s = d.createElement(t), options = {
'userName':'beingremarkable',
'formHash':'m2oq9xw0yusu89',
'autoResize':true,
'height':'828',
'async':true,
'host':'wufoo.com',
'header':'hide',
'ssl':true,
'defaultValues': 'field1=' + QueryString.cool};
s.src = ('https:' == d.location.protocol ? 'https://' : 'http://') + '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 { m2oq9xw0yusu89 = new WufooForm();m2oq9xw0yusu89.initialize(options);m2oq9xw0yusu89.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