MVC 5 – How to Close a Browser Tab Using Javascript

Close a windows tab upon returning a view:

    @if (ViewBag.CloseWindow == true)
    {
    <script type="text/javascript">
        window.close();
    </script>
    }

 Closing a Windows tab via a button click while on view:

<button style="font-weight:bold; margin-top: 5px;" type="button" onclick="CloseTab();" class="btn btn-primary">Close Tab</button>

    <script>
        function CloseTab() {
            window.close();
        }
    </script>
Please follow and like us: