My application's session management will redirect a user to a page where the user is told their session has timed out and please <a href="path/to/page.html" id="manual_link">sign in</a>
.
I'm trying to program the following behavior:
- If JavaScript is off (or otherwise not available), the user will have to click on the "sign in" link (POHTML; no problem).
If JavaScript is running:
a). progressively-enhance the link to instead submit the hidden form with an id of "security_redirect" (
document.getElementById('manual_link').onclick = "javascript:submit_security_redirect(); return false;";
)b) if the user does not click the link first, auto-submit the form after a 4 second delay (assume
remaining_time
to be 4000):setTimeout(function () { submit_security_redirect(); }, remaining_time);
The four-second-delay-then-submit is happening, but I can't seem to get the link to intercept if the user doesn't want to wait four seconds. In other words, if the user clicks the link after one second...nothing happens until the four seconds are up.
I suspect I need to kill the setTimeout on link click, but don't know how (my fumbling attempts at clearTimeout were so disappointing I don't even include them in the example JSFiddle). And/or I need to prevent the a
's default behavior (though I thought return false
squares that away).
What modifications are necessary to achieve the desired functionality?
You can't set
onclick
like that in the same way that you would set theonclick
attribute in HTML -- you need to set it to a function, e.g.:When adding event handlers via Javascript, calling
addEventListener
is actually better than settingonclick
, but unfortunately doesn't work in IE versions 8 and below, which is one of the reasons why cross-platform libraries like jQuery are great for event handling.BTW, arguments.callee is deprecated (see Arguments.callee is deprecated - what should be used instead?).