Advertisement
Guest User

Wufoo iframe javascript coolness

a guest
Feb 16th, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function WufooForm(){
  2. this.host='wufoo.com',
  3. this.userName='',
  4. this.formHash='',
  5. this.ssl='unset',
  6. this.autoResize=true,
  7. this.height='',
  8. this.width='100%',
  9. this.disabled=false,
  10. this.frameUrl='',
  11. this.defaultValues='',
  12. this.key='',
  13. this.polling=false,
  14. this.resizeDone='',
  15. this.initialize=function(params){
  16. for(key in params){
  17. this[key]=params[key];
  18. }
  19. this.key=this.formHash+''+Math.floor(Math.random()*1000000);
  20. this.generateFrameUrl();
  21. this.addEvent(window,'message',this.bindMethod(this.resizeWufooForm,this));
  22. },
  23. this.generateFrameUrl=function(){
  24. var url='';
  25. var refer = document.referrer;
  26. refer = refer.replace(/\//g, "wuslash");
  27. refer = refer.replace(/\+/g, "wube");
  28. url+=this.determineSecurity();
  29. url+=this.userName+'.'+this.host+'/embed/'+this.formHash+'/';
  30. url+='def/embedKey='+this.key;
  31. url += '&referrer='+encodeURIComponent(refer);
  32. if(this.defaultValues!='')url+='&'+this.defaultValues;
  33. if(this.ssl==false)url+='&secure=false';
  34. if(this.disabled)url+='&disable=true';
  35. if(this.autoResize==false)url+='&scrolling=true';
  36. this.frameUrl=url;
  37. },
  38. this.generateFullPageUrl=function(){
  39. var url='';
  40. url+=this.determineSecurity();
  41. url+=this.userName+'.'+this.host+'/forms/'+this.formHash+'/';
  42. if(this.ssl==false)url+='http/true/';
  43. return url;
  44. }
  45. this.addResizeScript=function(){
  46. if(!this.polling){
  47. var self=this;
  48. setTimeout(function(){
  49. var script=document.createElement("script");
  50. script.setAttribute("src",document.location.protocol+"//"+self.host+"/forms/height.js?embedKey="+self.key+"&variable="+self.formHash+"&timestamp="+new Date().getTime().toString());
  51. script.setAttribute("type","text/javascript");
  52. document.body.appendChild(script);
  53. },50);
  54. }
  55. },
  56. this.resizeForm=function(ret){
  57. if(this.autoResize==true&&ret.height){
  58. document.getElementById('wufooForm'+this.formHash).height=ret.height;
  59. if(ret.rules=='1'||this.isFunction(this.resizeDone)){
  60. if(this.isFunction(this.resizeDone))this.resizeDone(ret.height);
  61. __wufooCallBackFn=this.resizeDone;
  62. this.appendChildProxyFrame(ret.protocol);
  63. __poll(this.formHash);
  64. this.polling=true;
  65. }
  66. }
  67. },
  68. this.isFunction=function(object){
  69. return typeof object==="function";
  70. },
  71. this.determineSecurity=function(){
  72. if(this.ssl==true)return'https://';
  73. else return'http://';
  74. },
  75. this.generateFrameMarkup=function(){
  76. var scroll='no';
  77. if(this.autoResize==false)scroll='auto';
  78. var src='<iframe id="wufooForm'+this.formHash+'" height="'+this.height+'" allowTransparency="true" frameborder="0" scrolling="'+scroll+'" style="width:'+this.width+';border:none"'+
  79. 'src="'+this.frameUrl+'"><a href="'+this.generateFullPageUrl()+'" title="html form" rel="nofollow">Fill out my Wufoo form!</a></iframe>';
  80. return src;
  81. },
  82. this.display=function(){
  83. document.write(this.generateFrameMarkup());
  84. if(!window.postMessage)this.addEvent(document.getElementById('wufooForm'+this.formHash),'load',this.bindMethod(this.addResizeScript,this));
  85. },
  86. this.resizeWufooForm=function(event){
  87. if(event.origin!='http://'+this.userName+'.'+this.host&&
  88. event.origin!='https://'+this.userName+'.'+this.host){
  89. return;
  90. }
  91. if(this.autoResize==true){
  92. var data=event.data.split('|');
  93. var newFormHeight=new Number(data[0]);
  94. var formEl=document.getElementById('wufooForm'+this.formHash);
  95. if(formEl&&this.isMessageFromCorrectForm(data[1])){
  96. formEl.height=newFormHeight;
  97. }
  98. if(this.isFunction(this.resizeDone)){
  99. this.resizeDone(newFormHeight);
  100. }
  101. }
  102. },
  103. this.isMessageFromCorrectForm=function(dataHash){
  104. var hash=dataHash.substring(0,this.formHash.length);
  105. return(hash==this.formHash)?true:false;
  106. },
  107. this.addEvent=function(obj,type,fn){
  108. if(obj.attachEvent){
  109. obj["e"+type+fn]=fn;
  110. obj[type+fn]=function(){obj["e"+type+fn](window.event)};
  111. obj.attachEvent("on"+type,obj[type+fn]);
  112. }
  113. else{
  114. obj.addEventListener(type,fn,false);
  115. }
  116. },
  117. this.bindMethod=function(method,scope){
  118. return function(){
  119. method.apply(scope,arguments);
  120. }
  121. },
  122. this.appendChildProxyFrame=function(protocol){
  123. var proxyName='wufooProxyFrame'+this.formHash;
  124. __proxyFrameUrl=this.getChildProxyUrl(protocol);
  125. this.appendIFrame(proxyName,__proxyFrameUrl);
  126. },
  127. this.getChildProxyUrl=function(protocol){
  128. var url='';
  129. url+=protocol+'//';
  130. url+=this.userName+'.'+this.host+'/forms/blank.htm';
  131. url+='#'+window.location.href;
  132. return url;
  133. },
  134. this.appendIFrame=function(name,src){
  135. var proxyFrame=document.getElementById(name);
  136. if(!proxyFrame){
  137. var newiframe=document.createElement('iframe');
  138. newiframe.setAttribute('id',name);
  139. newiframe.setAttribute('name',name);
  140. newiframe.setAttribute('height','0px;');
  141. newiframe.setAttribute('style','position: absolute; left: -500px; top: 500px; width: 0px;');
  142. newiframe.setAttribute('src',src);
  143. document.body.appendChild(newiframe);
  144. }
  145. }
  146. }
  147. var __currentHeight=0;
  148. var __wufooCallBackFn='';
  149. function __poll(formHash){
  150. var childFrameHeight=__getChildFrameHeight();
  151. if((childFrameHeight>0)&&(__currentHeight!=childFrameHeight)){
  152. __currentHeight=childFrameHeight;
  153. document.getElementById('wufooForm'+formHash).height=childFrameHeight;
  154. if(typeof __wufooCallBackFn==="function"){
  155. __wufooCallBackFn(childFrameHeight);
  156. }
  157. }
  158. setTimeout("__poll('"+formHash+"')",500);
  159. }
  160. function __getChildFrameHeight(){
  161. var hArray=window.location.hash.split('=');
  162. return hArray[1];
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement