0

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?

Dr Upvote
  • 8,023
  • 24
  • 91
  • 204

4 Answers4

1

Use on('submit', function () { ... }):

$("form").on("submit", function () {
     window.location = "/redirectUrl";
     return false; // prevent refreshing page
});

JSFIDDLE

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>

JSFIDDLE

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
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;
});

REF window.open

silentw
  • 4,835
  • 4
  • 25
  • 45
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