html - Redirecting to URL in external Javascript function not working -
i'm trying program relatively simple site, html , javascript skills pretty basic. basically, i'm trying create page user asked question answer typing form. external javascript function users answer compared right one, , if they're same, user supposed redirected different site. here's form in html document:
<form name="myform" action="" method="get"> answer: <input type="text" name="inputbox" value=""> <button onclick="checkanswerexact(this.form)"> submit </button> </form>
my javascript function looks this:
function checkanswerexact(form){ //get answer var x = form.inputbox.value; //convert answer string var testansw =x.tostring(); //define right answer var rightansw = "rightanswer"; //check if same if (testansw.tolowercase() == rightansw.tolowercase() ){ alert("the answer right."); window.location.href = 'https://www.reddit.com/'; } //what if answer's right else { alert("the answer wrong."); } //what if answer's wrong }
as example site redirect to, chose reddit. alerts work right, comparison of strings okay. i've been googling while , read through bunch of forums nothing seemed work me. i've tried document.location=url
, , window.replace(url)
both inside if-else statement , outside , nothing seems work, window reloads. work window.open(url)
, don't want open in new tab. i'm using chrome browser, if that's relevant.
i'd love help, i've been researching hours , i'm pretty sure it's mistake made can't see.
cheers!
allright, figure out issue code. add return false on onclick event button. code become
<button onclick="checkanswerexact(this.form); return false;"> submit </button>
and change
window.location.href = 'https://www.reddit.com/';
to
window.location = 'https://www.reddit.com/';
Comments
Post a Comment