exemple le plus basique
<a href="#">lien pour adultes</a>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| function showWarning(url) {
const dialog = document.createElement('dialog');
document.body.append(dialog);
const msg = document.createElement('div');
msg.textContent = `attention, c'est pour adultes !`;
dialog.append(msg);
const btnCancel = document.createElement('button');
btnCancel.textContent = 'cancel';
dialog.append(btnCancel);
const btnConfirm = document.createElement('button');
btnConfirm.textContent = 'go !';
dialog.append(btnConfirm);
dialog.showModal();
btnCancel.addEventListener('click', () => {
dialog.remove();
}, {once: true});
btnConfirm.addEventListener('click', () => {
dialog.remove();
open(url, '_self');
}, {once: true});
}
document.querySelectorAll('a').forEach(link => {
link.addEventListener('pointerdown', e => {
e.preventDefault();
showWarning(link.href);
});
}); |
Partager