Created
February 27, 2015 12:10
-
-
Save toddheslin/41d54f115f495efb29e9 to your computer and use it in GitHub Desktop.
Advanced handling of Wufoo url query params
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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; | |
} (); | |
// Let's get the defaultValues properly! | |
var defaultValues = function() { | |
var defaultArray = []; | |
var defaultValues = ""; | |
// Define the URL paramters you would like to capture | |
// eg. {'field1' : QueryString.name} or {'field99' : QueryString.email} | |
var urlParams = [ | |
{'field1' : QueryString.cool}, | |
{'field2' : QueryString.awesome} | |
]; | |
// Let's loop thorugh and find which params we have today | |
for (var i = urlParams.length - 1; i >= 0; i--) { | |
var value = urlParams[i]; | |
var check = value[Object.keys(value)[0]]; | |
if(typeof check !== 'undefined') { | |
defaultArray.push(urlParams[i]); | |
} | |
}; | |
// Now we have those params, let's set the real 'defaultValues' | |
for (var i = defaultArray.length - 1; i >= 0; i--) { | |
var item = defaultArray[i]; | |
var key = Object.keys(item)[0]; | |
var prop = item[key]; | |
if (defaultValues === "") { | |
//defaultValues = "'" + key + '=' + prop + "'"; | |
defaultValues = key + '=' + prop; | |
} else { | |
var str = defaultValues.concat("&" + key + '=' + prop); | |
defaultValues = str; | |
}; | |
}; | |
return defaultValues; | |
}; | |
var defaultValues = defaultValues(); | |
console.log(defaultValues); | |
var m2oq9xw0yusu89;(function(d, t) { | |
var s = d.createElement(t), options = { | |
'userName':'beingremarkable', | |
'formHash':'m2oq9xw0yusu89', | |
'autoResize':true, | |
'height':'828', | |
'async':true, | |
'host':'wufoo.com', | |
'header':'hide', | |
'ssl':true, | |
'defaultValues': defaultValues}; | |
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