Within my HTML file, there exists a form containing a submit button as shown below:
<button id="submit">Submit</button>
In my JavaScript file, I wrote the following code:
window.onload = function() {
document.getElementById("submit").onclick = form_action;
}
function form_action() {
window.location.href = "www.google.com";
}
However, upon clicking the submit button, the redirect to another webpage does not occur. The only way I was able to make it work was by inserting alert(location.href)
after
window.location.href="www.google.com";
. It seems like this forces the window to update?
How can I resolve this issue without modifying my HTML file or utilizing JQuery? Why is this problem happening in the first place? Any insights and solutions using solely JavaScript would be greatly appreciated.
I am looking for a pure JavaScript solution and prefer not to alter my HTML file or resort to Jquery.