Search code examples
asp.net-corerazor

Is there a way to execute function before unloading razor view of _Layout asp.net core


I know that I can bind handler to onbeforeunload event and execute something before full page unload. But is there a way to do this on razor view of _Layout?


Solution

  • Yes,it could,If you add the js codes in _layout,when you leave the page,it would work I added the codes in my layout:

    <script type="text/javascript">
            window.onbeforeunload = function (){
                return("");
            }
        </script>
    

    when I leave the page, a dialogue pop out enter image description here

    You could share your codes if you have problems when you add the function of onbeforeunload