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

Time:09-21

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>

CodePudding user response:

You can't close browser tabs this simple.

I assume that you have a return button. What you can use target="_self" instead of target="_blank" so no tab will open and you can navigate back.

CodePudding user response:

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

  • Related