html - Redirect on form's submission with window.location.href -


trying out simple code, amazed didn't work, here go :

<form method="get" action="" >     <input type="submit" value="validate" onclick="redirect(); alertit(); "/> </form> <script>     function redirect(){        window.location.href = "test.html" ;     } </script> <script> function alertit(){    alert("redirect"); } </script> 

the code supposed redirect "test.html" on click of submit button, fails do. on other hand : alertit() works fine... question following : event handeling form have special rules should know ?

you need return false if want form not submit anyway.

function redirect(){        window.location.href = "test.html" ; } 

should be

function redirect(){     window.location.href = "test.html" ;     return false; } 

you should hook on submit event , not click event preferably, , return in handler.

onclick="return redirect(); alertit(); "  

would work (but not alert)

optimally, you'd want add id form example myform , attach event it.

document.getelementbyid("myform").addeventlistener("submit",function(){     alertit();     return redirect();  },false); 

Comments

Popular posts from this blog

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -