How to add conditional Script to load form only IF Cookie does NOT exist?

  • Profile Image
    Stefanoooo
    Asked on August 24, 2017 at 07:20 AM

    Hi There, do you offer any help for me if my aim is the following: conditional Script , only load JOTFORM IF  Cookie does NOT exist 

  • Profile Image
    ashwin_d
    Answered on August 24, 2017 at 09:09 AM

    Hello Stefanoooo,

    If I understand your requirement correctly, you mean to say that you want to check if cookies and if it does not exists, then only you want to show your form. Is that correct?

    Unfortunately it is currently not possible to add any script code in JotForm but with a little workaround it is possible to achieve your requirement. You need to add custom JavaScript code in your webpage where you want to display your form. 

    Please use the following script code in your webpage and see if that solves your problem:

    <script type="text/javascript">   

    checkCookie();

          //Set cookie

          function setCookie(cname, cvalue) {

            document.cookie = cname+"="+cvalue+"; ";

          }

          //Get cookie

          function getCookie(cname) {

              var name = cname + "=";

              var ca = document.cookie.split(';');

              for(var i = 0; i < ca.length; i++) {

                  var c = ca[i];

                  while (c.charAt(0) == ' ') {

                      c = c.substring(1);

                  }

                  if (c.indexOf(name) == 0) {

                      return c.substring(name.length, c.length);

                  }

              }

              return "";

          }

          //Check cookie is existing

          function checkCookie() {

              var user = getCookie("username");

              if (user == "visited") {

                /*What to do is the visitor is just returning (cookie already existing)*/

                 // alert("Welcome back!");

                    

              } else {

                /*If cookie is not yet set meaning visitor is first time to visit, then show this form*/

            <iframe id="JotFormIFrame-62463945265362" onload="window.parent.scrollTo(0,0)" allowtransparency="true" src="https://form.jotformeu.com/62463945265362" frameborder="0" style="width:100%; height:550px; border:none;" scrolling="no"> </iframe> 

     

    <script type="text/javascript"> var ifr = document.getElementById("JotFormIFrame-62463945265362"); if(window.location.href && window.location.href.indexOf("?") > -1) { var get = window.location.href.substr(window.location.href.indexOf("?") + 1); if(ifr && get.length > 0) { var src = ifr.src; src = src.indexOf("?") > -1 ? src + "&" + get : src + "?" + get; ifr.src = src; } } window.handleIFrameMessage = function(e) { var args = e.data.split(":"); if (args.length > 2) { iframe = document.getElementById("JotFormIFrame-" + args[2]); } else { iframe = document.getElementById("JotFormIFrame"); } if (!iframe) return; switch (args[0]) { case "scrollIntoView": iframe.scrollIntoView(); break; case "setHeight": iframe.style.height = args[1] + "px"; break; case "collapseErrorPage": if (iframe.clientHeight > window.innerHeight) { iframe.style.height = window.innerHeight + "px"; } break; case "reloadPage": window.location.reload(); break; } var isJotForm = (e.origin.indexOf("jotform") > -1) ? true : false; if(isJotForm && "contentWindow" in iframe && "postMessage" in iframe.contentWindow) { var urls = {"docurl":encodeURIComponent(document.URL),"referrer":encodeURIComponent(document.referrer)}; iframe.contentWindow.postMessage(JSON.stringify({"type":"urls","value":urls}), "*"); } }; if (window.addEventListener) { window.addEventListener("message", handleIFrameMessage, false); } else if (window.attachEvent) { window.attachEvent("onmessage", handleIFrameMessage); } </script>

     </script>    

                    setCookie("username", "visited");

              }

          }

    </script>

    Hope this helps.

    Do try it out and get back to us if you have any questions.

    Thank you!

  • Profile Image
    Stefanoooo
    Answered on August 28, 2017 at 08:43 AM

    Hi Ashwin_D,
    thank you for your reply
    I am very greateful, and yes you are right about the intention of my
    script.
    ...