简体   繁体   中英

Prevent users from opening link in new tab

I have a form that gets submitted between two websites, in the process taking the user from first-website.com to second-website.com (obviously not their real names)

On first-website.com I have a form like this:

<a href="#" class="submit-link">click here</a>
<form class="test-form" action="//second-website.com/gohere.php" method="post">
<input name="form-var" type="hidden" value="hello world">
</form>

When that submit-link is clicked, it triggers a bit of jQuery like this:

$(this).find('test-form').submit()

The problem is this only works when the link opens within the current browser window and tab. If the user right clicks the link and selects "Open link in new tab" (or clicks the centre mouse wheel, etc) then second-website.com doesn't correctly process the form. If they left-click the link it works fine and opens in the new tab, it's only an issue when the user deliberately tries to open a new tab or window.

So I need to make sure that users cannot open in a new tab, and if they use the centre mouse wheel click it will forcibly open within the current tab. Is this possible?

You could use the target="_blank" attribute in the form like this:

<form target="_blank">
    ...
</form>

This will allow the form to be submitted in a new tab.

You will need to use a regular submit button though:

<input type="submit" value="click here">

See this jsFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM