Your syntax is messed up. There are too many else statements.
This is the problem here:
} else {
//alert(tokenset);
alert('You have passed the token test');
}
EDIT
Based on your comments, it sounds like this is what you are trying to do: You want to be able to stop the user from leaving the page unless they do so through by clicking cancel.
In that case, you should be using the onbeforeunload
event. Here is the IE page, and here is the Mozilla page on the event.
The idea is that any time the user tries to leave the page (even just by closing the browser or typing some other address in the address bar), this function is called.
The function is limited. You can only return a string. The browser uses this to prompt the user If they hit cancel, then they stay on the page. They can hit OK and continue leaving the page. Although you can only return a string, you don't have to. You can check for a condition, and only return a string if the condition passes. Look my this example:
window.onbeforeunload = function () {
if (MyTokenIsNotSet) {
return 'You have not released the lock';
}
};
If your token is set, then the user will see nothing, and they will just leave the page. Otherwise, this will prompt the user to make sure they really want to leave the page.
A few of things to note:
- The prompt looks different in the different browsers (the wording is different). I would try it out to make sure your string looks OK with the rest of the prompt.
- This does not prevent the user from leaving. This only reminds the user that they SHOULD do something different. There is no way to reliably prevent the user form leaving a page.
- In addition to this, you should handle the event the user's session ends (on the server) and release the lock at that time.