I want call windows.onbeforeunload in an mvc application. I am using same laout page for all the views. I want to execute one logic on the tab/browser close. I tried adding in the layout page, but it gets called each time when moving to different view which uses same layout page. Can some one please suggest the proper place to call the function?