Intercept browser function based exit

Intercept browser function based exit

I want to produce a warning you are about to leave popup if the user tried to leave via browser based functions (back, forward, url bar, close tab, close browser ect)
but not if they use a page based function ( <a href>, form submit, javascript functions ect )

I have been trying to use code such as

  1. $(window).on( 'beforeunload' , function( event ) {
  2.       
  3.       if ( someCondition ) {
  4.             return 'please dont leave';      
  5.       }
  6.  
  7. });

In place of someCondition i have tried various pieces of code to attempt to establish if this or event contain a DOM object and only run the code if there it NOT a dom object present.

but so far i have had no luck

is is possible to do what i am attempting to do