Hi!
Could you tell me, if this is correct:
onclick="javascript: if(!confirm('Confirm delete?')) return false;"
Many thanks ;)
Hi!
Could you tell me, if this is correct:
onclick="javascript: if(!confirm('Confirm delete?')) return false;"
Many thanks ;)
Short answer: the technique is fine — confirm() is the browser dialog that returns true/false — but you do not need the javascript: prefix and there are cleaner, more robust options.
was right that the snippet works in browsers; if sees nothing happening then the problem is likely environmental (other JS errors, quoting/escaping when the HTML is generated, timing/CSP issues, or an element/id mismatch). Common checks: open the browser console for errors, try the control in a minimal standalone page to isolate it, and make sure any server-side templating is not breaking your attribute quoting. Note that and correctly flagged the Java vs JavaScript confusion earlier — this is plain JavaScript.
Prefer unobtrusive binding so handlers run when the DOM is ready and are not blocked by inline policy. Example pattern (attach after DOM ready and call preventDefault when needed):
document.addEventListener('DOMContentLoaded', function () {
var btn = document.getElementById('deleteBtn');
btn.addEventListener('click', function (e) {
if (!confirm('Are you sure you want to delete this?')) {
e.preventDefault();
}
});
}); For quick inline cases, returning false from an onclick will cancel the default action, but for maintainability use event listeners and non-blocking custom modals for better UX. See MDN on the built-in confirm dialog and on attaching listeners for details: Window.confirm and EventTarget.addEventListener.
Jump to Post— stultuske 1,129you do realise that JavaScript and Java are not the same thing, do you?
Jump to Post— masijade 1,351Seemingly not.
you do realise that JavaScript and Java are not the same thing, do you?
Seemingly not.
Moved to the correct forum.
I dont see any problems with the snippet of code.
Are u getting any errors?
Thank you very much for the reply, Thirusha!!!
Unfortunately, yes - it just doesn't want to load it :( And i don't know what to do...
I have tested it on a button like this: (it works both in IE6 and FF3)
<input type="button" name="tbutton" id="tbutton" value="testbutton" onclick="javascript: if(!confirm('Confirm delete?')) return false;" /> It then pops up a confirmation box.
Can u provide your code, maybe something else is breaking first and thats why that part doesnt work.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.