Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to flag onclick() and confirm() using DOM

Status
Not open for further replies.

donpro

IS-IT--Management
Joined
Jun 15, 2004
Messages
2
Hi,

I have an session application where the user logs out via a simplt logot link. The code to do this is:
<a id="logout" title="Log out" href="logout.php" onClick="return confirm('Do you really want to log out?')">Logout</a>

So if the user answers YES to the confim, logot.php is run and the user is logged ot. If the user answers NO, they remain on the page.

I removed the onClick() and tried to use DOM but I find that with my code, the user gets logged ot regardless of whether they choose YES or NO so obviosly, my code is incomplete. I present it below, if anyon can help, I wodl appreiciate it.

HTML
-------
<a id="logout" title="Log out" href="logout.php">Logout</a>

JavaScript
========
function addEvent(elm, evType, fn, useCapture) {
if (elm.addEventListener) {
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent) {
var r = elm.attachEvent('on' + evType, fn);
return r;
} else {
elm['on' + evType] = fn;
}
}

function handleClick(e) {
return confirm('Do you really want to log out?');
}

function addListeners() {
if (!document.getElementById) {
return;
}

var logout_link = document.getElementById('logout');
addEvent(logout_link, 'click', handleClick, false);
}

addEvent(window, 'load', addListeners, false);
 
There are slightly more involved consideration than you have thought in the return structure.
[tt]
function handleClick(e) {
[red]//[/red]return confirm('Do you really want to log out?');
var b=confirm('Do you really want to log out?');
if (!b) {
try { e.preventDefault();} catch(err) { window.event.returnValue=false;};
}
//return b; //doesn't really matter anymore
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top