I'm using this code to have contact form submissions sent to a google spreadsheet. Between lines 89-99 I added the following to redirect to a thank-you page instead of displaying a thank-you div:

if (!validEmail(data.email)) {   // if email is not valid show error
    document.getElementById('email-invalid').style.display = 'block';
    return false;
} else {
    var url = event.target.action;  //
    var xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    // xhr.withCredentials = true;
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.onreadystatechange = function() {
        console.log( xhr.status, xhr.statusText )
        console.log(xhr.responseText);
        //document.getElementById('gform').style.display = 'none'; // hide form
        // document.getElementById('thankyou_message').style.display = 'block'; 
        window.location.href = "mensaje-enviado.html";
        return;
    };

Here's a working link, can someone advise what I'm doing wrong?

Thank you in advance!

1 Answers

0
Aman On

You can create a anchor tag element with target set to "_blank" in your HTML with display none

<a id="thankYouLinkId" href="thankYouLinkUrl" target="_blank" style="display: none;">Test Link</a>

Now use javascript code to open the thankYouLinkUrl in new tab:

document.getElementById("thankYouLinkId").click();