How to catch beforeunload and stop page to reload without alert

unfortunately after some testing I’v found that catching click is a fragile solution because any click is catched, like click some ajax link which adds some items to my form, or just clicking textarea to fill it etc … at least when I’v tried click event, no turbo.
I’d rather prefer some solution based only on beforeunload/onbeforeunload, if possible