title: Create URL with cookies from a different page and redirect with JS
body: <p>I am newbie in JS but I managed to create the code thanks to the feedback from users of this site. Explain the code first:</p>
  
  <p>The objective is to create a URL link basing on two pages (A and B).</p>
  
  <p>The page A loads a cookie using a hidden form. This page also contains a link that redirects to page B:</p>
  
  <pre><code>// CODE FROM SOURCE PAGE A //
  &lt;a href="to_page_b"&gt;TO PAGE B&lt;/a&gt;
  
  &lt;form name="sender"&gt;
  &lt;input type="hidden" name="message" size="30" value="customvalue"/&gt;
  &lt;/form&gt;
  &lt;script type="text/javascript"&gt;
  function setCookie(value) {
      document.cookie = "cookie-msg-test=" + value + "; path=/";
      return true;
  }
  function updateMessage() {
      var t = document.forms['sender'].elements['message'];
      setCookie(t.value);
      setTimeout(updateMessage, 100);
  }
  updateMessage();
  &lt;/script&gt;
  </code></pre>
  
  <p>Page B has a list of links with different values assigned. With JS call the cookie from page A and assign it to a variable. When we click on a link assign the value to another variable. JS creates the new URL with these two variables and redirects.</p>
  
  <pre><code>// CODE FOR DESTINATION PAGE B //
  &lt;a id="LNK" href="#" value="value_for_url" onclick="clickLink(this)"&gt;CUSTOM REDIRECTION&lt;/a&gt;
  &lt;a id="LNK" href="#" value="other_value_for_other_url" onclick="clickLink(this)"&gt;OTHER CUSTOM REDIRECTION&lt;/a&gt;
  // RECEIVE THE COOKIE FROM SOURCE PAGE A //
  &lt;script type="text/javascript"&gt;
  function getCookie() {
      var cname = "cookie-msg-test=";
      var ca = document.cookie.split(';');
      for (var i=0; i &lt; ca.length; i++) {
          var c = ca[i];
          while (c.charAt(0)==' ') c = c.substring(1,c.length);
          if (c.indexOf(cname) == 0) {
              return c.substring(cname.length, c.length);
          }
      }
      return null;
  }
  // PASTE THE COOKIE AND THE VALUE OF PAGE A AND REDIRECT //
  function clickLink(a) {
      var url_part_from_a = a.getAttribute('value');
      var url_part_from_cookie = getCookie();
      window.location.assign("http://domain/"+url_part_from_a+url_part_from_cookie)
         return false;
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>As will be different pages A with different valors of cookies I would modify the page A code so that when you click on the link assign the value to the cookie and not before.</p>
  
  <p>Any ideas? Thanks in advance</p>