0

In my HTML file i have a form that has the following submit button:

<button  id="submit" >Submit</button>

In my Javascript file i did the following :

window.onload=function(){
    document.getElementById("submit").onclick = form_action;
}

function form_action()
{
    window.location.href="www.google.com";
}

So, the submit button redirects to another webpage. ( I read some data from a form, but that doesn't matter right now, and after that i redirect to another page). The thing is, the redirect does not happen at all!

The only way i was able to make it work was by adding : alert(location.href) after window.location.href="www.google.com"; . My guess is that this forces the update of the window?

How do i work around this problem? Why does this occur? Can someone offer some insight? I want to solve it purely by using Javascript.

I do not wish to modify my HTML file nor use Jquery.

Eduard6421
  • 295
  • 1
  • 11

1 Answers1

4

The default "type" of a <button> is "submit". Thus your button was submitting the form it's located in, to whatever URL that form is set to. By adding type="button", the <button> will have no built-in action so your JavaScript code will be all that matters.

Pointy
  • 405,095
  • 59
  • 585
  • 614