Ad

Open A Link In New Tab And Trigger Postback In The Current Tab

- 1 answer

I want to dislay a link button that, when clicked, triggers postback in the current tab and opens a link in new tab. If it matters, the backend is Python Django.

Ad

Answer

<script type="text/javascript">
    function submitAndOpen() {
        document.getElementById('form1').submit();
        return true;
    }
</script>

<form method="POST" id="form1">
    <a target="_blank" rel="nofollow noreferrer" href="www.example.com" target="_blank" onclick="return submitAndOpen()">
        Submit and open in new tab
    </a>
</form>

So returning true in onclick says the browser to follow the link.

target="_blank" says to open the link in a new tab.

The JavaScript function also submits the form.

Ad
source: stackoverflow.com
Ad