0

I was trying to submit the form data to https://wallpapersforandroid.com/wallpaper-4/" in new tab and redirect the parent page to "https://www.youtube.com" after submission. I do success to open the action page in new tab but the parent page is not redirecting The code I used:

<form action="https://wallpapersforandroid.com/wallpaper-4/" method="post" target="_blank">
Type Password From Below Image: <input name="pass" type="text" />
<input id="myForm" type="submit" /><br />
<script>
  document.getElementById("myForm").onsubmit = function() {
    window.location.href = "https://www.youtube.com";    
  };
</script>
5
  • 2
    You're trying to send the user to two different places. One is the form's action, the other is YouTube. Which one do you want to send the user to? If the latter then you'll need to submit the form via AJAX instead of directly and then perform your redirect after that operation completes. Commented May 29, 2020 at 14:53
  • just try window.location = "https://www.youtube.com" Commented May 29, 2020 at 14:55
  • 1
    @Doc-Han: That would fail the same way and for the same reason. Commented May 29, 2020 at 14:55
  • it's worth trying. give it a try Commented May 29, 2020 at 14:57
  • 1
    @Doc-Han - No, it's not. :-) It's a total red herring. Commented May 29, 2020 at 14:58

1 Answer 1

1

If you submit the form, the current page is torn down and replaced with the result from submitting the form. If you set a new location, the current page is torn down and replaced with the page from that location. You can't do both at once; one of them wins and the other will lose.

You can submit your form via ajax and then do the redirect:

document.getElemntById("myForm").addEventListener("submit", function(e) {
    // Prevent the default form submission
    e.preventDefault();

    // Get the form data
    const data = new FormData(this);

    // Do the ajax
    fetch(this.action, {
        method: this.method,
        body: data
    })
    .then(response => {
        if (!response.ok) {
            throw new Error("HTTP error " + response.status);
        }
        window.location.href = "https://www.youtube.com";
    })
    .catch(error => {
        // ...handle/report error...
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

can't i do this via html and java script only. I don't know anything about AJAX.
@AshishSah - The only other way would be for the page returned by the form submission to do the redirect, instead of this page. But note that there's nothing special about the above, it's all stuff built into all modern browsers (except that fetch isn't on IE; you can polyfill it, though, or use XMLHttpRequest instead). And your server part is unchanged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.