title: JavaScript code that's supposed to return the user to a login page if a cookie doesn't exist, doesn't work
body: <p>So I have multiple pages connected that should check if a cookie exists when first visited, as the cookie wont exist it should send the person to the login page where after put in their username, password and time limit for the cookie, the script creates a cookie and redirects them back to the index page. Now everything works besides the redirecting part, because I'm guessing its not fetching the cookie right so it keeps sending you to the login page as soon as you get redirected to where you were supposed to.</p>
  <pre><code>function upisi_cookie() {
  var danas = new Date();
  var istice = new Date();
  var username = document.prijava.username.value;
  var password = document.prijava.password.value;
  var trajanje = parseInt(document.prijava.trajanje.value);
  
  istice.setTime(danas.getTime() + (trajanje * 1000));
  let expires = &quot;expires=&quot; + istice.toUTCString();
  document.cookie = &quot;User=&quot; + username + &quot;;&quot; + expires + &quot;;path=/&quot;;}
  </code></pre>
  <p>Above is the cookie creation code. And this is for fetching the cookie and checking weather it exists or not (and redirection if it does exist)</p>
  <pre><code>function getCookie(User) {
      var dc = document.cookie;
      var prefix = User + &quot;=&quot;;
      var begin = dc.indexOf(&quot;; &quot; + prefix);
      if (begin == -1) {
          begin = dc.indexOf(prefix);
          if (begin != 0) return null;
      }
      else {
          begin += 2;
          var end = document.cookie.indexOf(&quot;;&quot;, begin);
          if (end == -1) {
              end = dc.length;
          }
      }
      return decodeURI(dc.substring(begin + prefix.length, end));
  }
  window.onload = function ControlCookies() {
      var myCookie = getCookie(User);
      stranicaCurrent = document.getElementsByTagName(&quot;title&quot;);
      if (myCookie == null &amp;&amp; stranicaCurrent != &quot;prijava&quot;) {
          window.location.href = &quot;---login page location---&quot;; //sends the user to a login page if there are no cookies
      }
      else {
      }
  }
  
   
  </code></pre>