This code below for some reason doesn't work when trying to redirect. I have tried several other ways such as window.location.href and more. The only thing that works is window.open ("")
but this opens a page in a new window when I need it to be on the same. If I do window.open("", "_self")
then it does not work again. If I replace the window.location
with an alert
it works fine, so I think the overall code is normal just something preventing it from redirecting on the same page. This issue is also on my Windows Chrome and a Mac Firefox.
<html>
<head>
<script type="text/javascript">
function checkAnswers(){
getElementById("myQuiz");
if(myQuiz.elements[1].checked){
window.location = "www.google.com";
}else{
alert("Failed");
}
};
</script>
</head>
<body>
<img src="Castle.JPG" />
<form id="myQuiz" onsubmit="checkAnswers()" action="">
<p>Name the country this castle is located in?
<br/>
<input type="radio" name="radiobutton" value="A"/>
<label>England</label>
<input type="radio" name="radiobutton" value="B"/>
<label>Ireland</label>
<input type="radio" name="radiobutton" value="C"/>
<label>Spain</label>
<input type="radio" name="radiobutton" value="D"/>
<label>France</label>
<input type="submit" name="submit" value="Submit"/>
<input type="reset" name="reset" value="Reset"/>
</p>
</form>
</body>
</html>
change js to:
function checkAnswers()
{
var myQuiz=document.getElementById("myQuiz");
if (myQuiz.elements[1].checked) {
window.location.href = "http://www.google.com";
}
else {
alert("Failed");
}
return false;
};
and change:
<form id="myQuiz" onsubmit="checkAnswers()" action="">
to
<form id="myQuiz" onsubmit="return checkAnswers();" action="">