A
A
Abduvohid Akhmedov2016-02-20 09:40:12
JavaScript
Abduvohid Akhmedov, 2016-02-20 09:40:12

How to save cookies on all browsers?

Hi all! Created a static site for coursework. And on the site, I used javascript to save cookies and if the user is not logged into the site, he will be redirected to the login page.
The problem is this: Mozilla Firefox saves cookies and the site works fine. But on all other browsers, cookies are not saved. I tried to open the site through the local disk (file:///D:/Documents/CW/login.html) and through OpenServer ( site.ru/login.html).
Here is the js code:

function Login() {
  if (document.getElementById('log').value == "User" && document.getElementById('pass').value == "User123") {
    var cookie_date = new Date();
    cookie_date.setFullYear(2018);
    if (document.getElementById('save').checked == true){
      document.cookie = "Logged=true; path=/; expires=" + cookie_date.toGMTString();
    } else {
      document.cookie = "Logged=true; path=/; expires=;";
    }
    document.getElementById("success").style.display = 'block';
    window.setTimeout(function(){
      window.location.href = "index.html";
    }, 1000);
  } else {
    document.getElementById("error").style.display = 'block';
  }
}

And the html code:
<form name="login">
      <div class="logo">
        <a class="logo_l" href=""></a>
      </div>
      <input type="text" maxlength="12" id="log" placeholder="Login" value="User">
      <input type="password" maxlength="12" id="pass" placeholder="Password" value="User123">
      <div class="last">
        <input type="checkbox" id="save">
        <label for="save" class="save">Keep me logged in</label>
        <a onclick="Login()" class="login">Log in</a>
      </div>
    </form>
    <span id="error">Login or password is not correct!</span>
    <span id="success">You successfully logged in!</span>

How can this problem be solved?
Thanks in advance for your replies!

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
dark hole, 2016-02-20
@abyrkov

Is using LocalStorage not an option?

M
Mikhail Rybalka, 2016-02-20
@RuSPanzer

W0XKYruL.png
Google Chrome, your code is working.

A
Alexey, 2016-02-20
@alsopub

Alternatively, maybe you have a problem not with setting, but with reading cookies?
Check via alert(document.cookie) in get_cookie().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question