Problem solved itself...
FYI: Apparently that was Mac OS or Chromium engine bug as after last system and browser updates problem disappeared as suddenly as it appeared before. It looks like there it is not relevant to jQuery or JavaScript API.
I'll keep this question for few days and I'll delete it later as it doesn't fit StackOverflow rules in such situation.
Original post:
I have simple JS confirm inside the on click listener, exactly like in code below, no more requests (i.e. AJAX, no duplicated JS includes etc...).
The problem is that confirm dialog is fired twice in some browsers (i.e. Chrome and Opera at Mac OS) and my question is, did I miss something obvious or is that considerable bug of the mentioned browsers? Is there any workaround which will help me to prevent this? Also tried with e.preventDefault();
also with no luck.
Edit I should've also mention that if there's no confirm('...');
within the on click it's fired only once (so only one Let us check... in the console occurs). With confirm both logs appears twice in the console, but boolean is in opposite order of clicking - for an sample - if I'll click Cancel
first and OK
later console logs true
first and false
at the end... quite weird :S
Versions:
- jQuery version: 1.11.3 - 1.12.0
- Chrome: 47.0.x (newest)
- Opera: 34.0.* (newest)
Sample code (complete):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test...</title>
<script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
</head>
<body>
<div class="myBtn" data-field="one">Take one</div>
<div class="myBtn" data-field="two">Take two</div>
<div class="myBtn" data-field="three">Take three</div>
<div class="myBtn" data-field="four">Take four</div>
<script>
$(document).ready(function () {
$(document).on('click', '.myBtn', function () {
console.log('Let us check...');
var confirmation = confirm('Are you sure you want ' + $(this).data('field') + '?');
console.log(confirmation);
});
});
</script>
</body>
</html>
Or just click here: https://jsfiddle.net/gu58wwtg/1/
Last edit: just checked other possibilities like bare approach (with pure JS, no jQuery) and also prompt('...');
(which also opens twice in reversed order) instead of confirm('...');
and problem still occurs, so I suspect a bug, sorry, for bothering you, and thanks for your attention. I'll keep the question, so other guys can save some time on frustrations ;)