Search code examples
javascriptphonegap

JavaScript Logout Function (PhoneGap Mobile Development)


I am a newbie to coding and I am coming across an issue with my logout function which I have pasted below.

function logout()
{
  localStorage.setItem("data", data);
  var user = localStorage.getItem("data");
  user.rows.item(0).name;
  localStorage.removeItem("data");
  window.location.href = "signin.html";

}

I am calling it via this html fuction:

<a class="btn btn-primary btn-lg btn-block" onclick="logout()" href="#"><span><i class="fa fa-sign-out-alt"></i></span> Logout</a>

What I am trying to do is when the user clicks on the logout button, on click it will invoke a function in which it will remove the data of the user from the local storage and direct them back to the sign in page.

I'm not sure where I have gone wrong here. I'd like to know if I've written it wrong so I can learn from my mistakes and improve on my JavaScript.

Thank you :)


Solution

  • Try something like this:

    $(document).ready(function() {
            $("#logout").click(function(){
                localStorage.login="false";
                localStorage.removeItem("username");
                window.location.href = "index.html";
            });
    });