I'm building a simple chrome extension. In it, I'm trying to make the submit button in a form redirect the user to a different page after submitting the form but I can't get it to work. I have tried the following:
//First way
const button = document.querySelector('.button');
button.addEventListener('click', () => {
document.location.href = 'results.html';
});
//Second way
const form = document.querySelector('form');
form.onsubmit = redirect;
function redirect() {
location.href = 'results.html';
console.log("successfully redirected");
};
//Third way
<button onclick="window.location.href='results.html'" class="button" type="submit">Submit</button>
//Fourth way
<a href="results.html">
<button class="button" type="submit">Submit</button>
</a>
My form looks like this
<form method="POST" action="http://localhost:3000/sleep">
<label> How much did you sleep last night?
<input name="duration" type="number">
</label>
<label> How well did you sleep?
<div>
<label>
<input type="radio" name="quality" value="bad">
Bad
</label>
<label>
<input type="radio" name="quality" value="ok">
Ok
</label>
<label>
<input type="radio" name="quality" value="good">
Good
</label>
</div>
</label>
<input type="hidden" name="user_id" value="1">
<button class="button" type="submit">Submit</button>
</form>
I tried redirecting to a page using different elements other than the submit button and that somehow worked. Is there something about the submit button that doesn't allow me to redirect? I am trying to make the user experience a little better by redirecting as soon as the form submits so I would like to redirect using the submit button if that's possible. Also, I don't know if this is relevant but I am using manifest v3.
Use in this way
function clickHandler(e) { chrome.tabs.update({url: "https://stackoverflow.com"}); window.close(); // Note: window.close(), not this.close() } document.addEventListener('DOMContentLoaded', function() { document.getElementById('button').addEventListener('click', clickHandler); });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.