title: How to prevent code from executing before a JQuery plugin is finished saving a cookie
body: <p>I have the following function called HowTo that is executed with a onclick event, a function within it requires the JQuery plugin <a href="https://github.com/carhartl/jquery-cookie" rel="nofollow">jquery.cookie</a> which is: "<em>A simple, lightweight jQuery plugin for reading, writing and deleting cookies.</em>" :</p>
  
  <pre><code>function HowTo(){
  
     $largeAmountOfHTML = document.getElementById("element").innerHTML;
  
     $.cookie("nameOfCookie", $largeAmountOfHTML);
  
     $.ajax({
         type: "GET",
         url: "path/to/serverside/script.php",
         async:false,
     });
  
  
     window.location.href='anotherPage.php';
  
  };
  </code></pre>
  
  <p>The function firstly retrieves a substantial amount of HTML and assigns it to the variable $largeAmountOfHTML. Then using the JQuery function $.cookie() declared in <a href="https://github.com/carhartl/jquery-cookie" rel="nofollow">jquery.cookie</a>'s library the function saves the value of $largeAmountOfHTML to the cookie nameOfCookie. Then the function makes an AJAX call that executes a serverside php script which retrieve the cookie data and saves the content to a MYSQL database. Once complete the function directs the current window to anotherPage.php.</p>
  
  <p>The function runs smoothly with no console errors declared. However, when $largeAmountOfHTML reaches a certain size <a href="https://github.com/carhartl/jquery-cookie" rel="nofollow">jquery.cookie</a> cannot assign the cookie fast enough before the serverside script attempts to retrieve it. <a href="https://github.com/carhartl/jquery-cookie" rel="nofollow">jquery.cookie</a> does not list any methods I can use to prevent this from happening.</p>
  
  <p>Once $largeAmountOfHTML is large enough and cannot be saved to the cookie in time the severside script declares the this error:</p>
  
  <pre><code>&lt;br /&gt;
  &lt;b&gt;Notice&lt;/b&gt;:  Undefined index: nameOfCookie in &lt;b&gt;localhost/path/to/serverside/script.php&lt;/b&gt; on line &lt;b&gt;4&lt;/b&gt;&lt;br /&gt;
  </code></pre>
  
  <p>What is the best way to get around this?</p>