Search code examples
htmlcssasp.net-mvcrazormenuitem

How to close new tab on clicking button which was opened with _blank <a> target attribute


I am using the code below to open a new tab upon clicking a bootstrap button in a razor .cshtml page currently. I have a button in the new tab opened which returns to the main menu, but I would like to know how to close this tab, to effectively return to where the user was previously.

<ul class="nav nav-sidebar">
    <!-- Menu Item -->

    <li class="menuItem">
        <a asp-controller="New Tab Section Menu" asp-action="Index" target="_blank" class="menuItemLabel">
            <i class="fas fa-book-reader fa-lg" style="padding-right: 5px"></i>
            New Section Menu
        </a>
    </li>

Solution

  • You could use window.close() in Javascript before, but this is mostly unavailable EXCEPT when you open a new tab with target="_blank" or by script.

    In your case it might be worth a try. Use a button with:
    <a href="#" onclick="window.close()">Return to base!</a>

    Browsers are very picky about this for security and useability reasons.

    https://stackoverflow.com/a/66688958/6803592