I'm currently trying to program a relatively simple site, but my HTML and Javascript skills are pretty basic. Basically, what I'm trying to do is create a page where the user is asked a question which they answer by typing it into a form. With an external Javascript function the users answer is compared to the right one, and if they're the same, the user is supposed to be redirected to a different site. Here's the form in my 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 like this:
function checkAnswerExact(form){
//GET ANSWER
var x = form.inputbox.value;
//CONVERT ANSWER TO STRING
var TestAnsw =x.toString();
//DEFINE RIGHT ANSWER
var RightAnsw = "rightanswer";
//CHECK IF THEY ARE THE SAME
if (TestAnsw.toLowerCase() == RightAnsw.toLowerCase() ){
alert("The answer is right.");
window.location.href = 'https://www.reddit.com/';
} //WHAT TO DO IF ANSWER'S RIGHT
else {
alert("The answer is wrong.");
} //WHAT TO DO IF ANSWER'S WRONG
}
As an example site to redirect it to, I chose reddit. The alerts work right, so the comparison of the strings is okay. I've been googling for a while now and read through a bunch of forums but nothing seemed to work for me. I've tried document.location=url
, and window.replace(url)
both inside the if-else statement and outside and nothing seems to work, the window only reloads. What does work is window.open(url)
, but I don't want it to open in a new tab. I'm using Chrome as my browser, if that's relevant.
I'd love some help, I've been researching for some hours and I'm pretty sure it's probably some mistake I made that I can't see.
Cheers!