user23792
user23792

Reputation: 43

Javascript form submit open new window tab, and then redirect parent page

When I submit the form I'm trying to open a new window tab and pass the values over into the new window tab. Then I need the parent page to redirect to the next page.

The problem is the form values are not passing to the new window tab. How could I do that with the following code I have right now?

<form class="form" id="promo_form" method="post">
<input type="text" name="first_name" id="first_name">
<input type="text" name="last_name" id="last_name">
<input type="text" name="zipcode" id="zipcode" maxlength="5">
<button type="submit" id="promo_form_button" class="submit">Continue</button>
</form>


$(document).ready(function($){

var form = document.getElementById("promo_form");

    function goToUrl(){
        window.open("https://site.com/new-tab", "_blank");
        window.location = "https://site.com/parent-next-page"
    }

    document.getElementById("promo_form_button").onclick = goToUrl;
});

Upvotes: 4

Views: 10226

Answers (1)

epascarello
epascarello

Reputation: 207557

Why use window.open when a form can target a new tab/window itself.

HTML:

<form target="_blank" method="get" action="http://google.com" id="myForm">
    <label for="q">Search: </label><input type="text" id="q" name="q" />
    <input type="submit" value="search" />
</form>

JavaScript:

document.getElementById("myForm").onsubmit = function() {
    window.location.href = "http://www.jsfiddle.net";
    return false;
};

Example:

JSFiddle

Upvotes: 4

Related Questions