title: Check cookie based on form submission
body: <p>I have an embedded form on a page.  When the page loads, it needs to check if the user is cookied.  If the user is cookied, alert them.  If not, allow the person to submit the form and then cookie them.  With the code below, upon page load, the user is cookied and always looks like a return user despite only invoking the cookieing function upon submit.</p>
  <pre><code>&lt;body onLoad=&quot;checkCookieExists()&quot;&gt;
  &lt;h1&gt;Cookie test&lt;/h1&gt;
  
  &lt;p&gt;When the form loads, it will tell you if you've been here before based on the cookie.&lt;/p&gt;
  
  &lt;form id=&quot;myForm&quot; action=&quot;/&quot;&gt;
    Enter name: &lt;input type=&quot;text&quot; name=&quot;fname&quot;&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  
  &lt;script&gt;
  function checkCookieExists() {
        if (document.cookie.indexOf('front-row=submitted')== -1 ) {
         // New user
          alert(&quot;this is your first time&quot;);
            console.log(&quot;existcookienew&quot;);
      }
         else {
        // They've been here before.
          alert(&quot;hello again&quot;);
  
      }
  }
  
  document.getElementById(&quot;myForm&quot;).addEventListener(&quot;submit&quot;, checkCookie());
  
  function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = &quot;expires=&quot; + d.toGMTString();
    document.cookie = cname + &quot;=&quot; + cvalue + &quot;;&quot; + expires + &quot;;path=/&quot;;
  }
  
  function checkCookie() {
       setCookie(&quot;front-row&quot;, &quot;submitted&quot;, 30);
  }
  
  &lt;/script&gt;
  '''
  </code></pre>