Javascript - Open a given URL in a new tab by clicking a button

前端 未结 13 433
一生所求
一生所求 2020-12-04 13:35

I would like to have a button that redirects to a given URL and opens in a new tab. How can this be done?

相关标签:
13条回答
  • 2020-12-04 14:41

    You can forget about using JavaScript because the browser controls whether or not it opens in a new tab. Your best option is to do something like the following instead:

    <form action="http://www.yoursite.com/dosomething" method="get" target="_blank">
        <input name="dynamicParam1" type="text"/>
        <input name="dynamicParam2" type="text" />
        <input type="submit" value="submit" />
    </form>
    

    This will always open in a new tab regardless of which browser a client uses due to the target="_blank" attribute.

    If all you need is to redirect with no dynamic parameters you can use a link with the target="_blank" attribute as Tim Büthe suggests.

    0 讨论(0)
提交回复
热议问题