I’m validating a form and after validation, I want to redirect with window.location = xxx but it doesn’t work unless I put an alert call after it.
The validation works by handling the onsubmit event of the form.
function isvalid()
{
var txt = document.getElementById('txtTitle');
if (txt.value == '')
{ alert('Please enter a title'); return false;}
else
{
var sURL = "get_movies.php?title=" + txt
window.location.href = sURL;
//alert('');
return true;
}
}
The form’s
onsubmitexpects either a true or false return value. If you return true, the form will be submitted. Since you want to manually set thewindow.locationon a form submit, it doesn’t make sense to return true (you will then have the page fighting over a form submit and a JS redirect.Put
return falsein your else block and you should be good to go.