I would like to create something like 'if form submits' redirect user opening a new tab, to a specific URL. I would like to write this as a JavaScript function, any suggestions?
Asked
Active
Viewed 153 times
4 Answers
1
Use on('submit', function () { ... })
:
$("form").on("submit", function () {
window.location = "/redirectUrl";
return false; // prevent refreshing page
});
Most of browsers will block the opening of a new tab, so, just redirect the user on the new page.
But you can do it with HTML:
<form target="_blank" action="/redirectUrl"></form>

Community
- 1
- 1

Ionică Bizău
- 109,027
- 88
- 289
- 474
-
`window.open(url, '_blank');` – karaxuna Aug 12 '13 at 14:55
-
@karaxuna Yes, I pasted the link. Most of browsers block new tab opening... – Ionică Bizău Aug 12 '13 at 14:56
1
You can write target attribute to form tag:
<form target="_blank"></form>

karaxuna
- 26,752
- 13
- 82
- 117
0
$('form').on('submit', function() {
window.open('someurl');
// Some code to do the submit
// Prevent the submit
return false;
});

silentw
- 4,835
- 4
- 25
- 45
-
Sorry, but I have to say you that [W3Schools is bad](http://www.w3fools.com/). – Ionică Bizău Aug 12 '13 at 14:59
-
@Johnツ I know that, but it has the correct info for this particular resource. – silentw Aug 12 '13 at 15:00
0
If I understood you, you need to open a new tab with an specific url, a part of the regular behaviour of the form submission.
Major browsers blocks opening a new window/tab on a non-user-click event. So you need to bind an extra event on the submit button, but then you can't control if submission was successful (for example having a validator like jquery.validator).
$("#myform input[type=submit]").click(function() {
window.open("http://www.site.com/");
});

kpull1
- 1,623
- 15
- 19