Thursday, 22 August 2013

Javascript Check Login function returns false, but continues to next line anyways?

Javascript Check Login function returns false, but continues to next line
anyways?

The following code checks for a user and returns false if the user is not
logged in.
function checkLogin(e) {
loggedIn = $('#loggedin').text();
if(loggedIn == '1'){
e.preventDefault();
alert("Please Log In");
return false;
}
}
I then call this function before some other code and an ajax call.
tileFavorite = $('.tileFavorite');
tileFavorite.on('click', function(e) {
checkLogin(e);
//some code goes here before ajax call
$.ajax({
url: // ajax call goes here,
cache: false
})
});
The check login actually works. However if the checkLogin function returns
false, it still continues to the code beneath it. There a way to avoid
this?

No comments:

Post a Comment