Load Webpage in JS condition
-
I have the following code and if the condition is true it should load a web page but it dont. Help would be appreciated: Enter your aggretate for your Matric final exam First name: Last name: function myFunction(){ var fname = document.getElementById("fname").value; if(fname > 40); window.location.href = "http://www.w3schools.com"; else alert("Condition xxxxx met") }
-
I have the following code and if the condition is true it should load a web page but it dont. Help would be appreciated: Enter your aggretate for your Matric final exam First name: Last name: function myFunction(){ var fname = document.getElementById("fname").value; if(fname > 40); window.location.href = "http://www.w3schools.com"; else alert("Condition xxxxx met") }
This is very easy to debug, but only you can do it. We don't have access to your code. What is the value of fname when the if statement is hit?
Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.
-
I have the following code and if the condition is true it should load a web page but it dont. Help would be appreciated: Enter your aggretate for your Matric final exam First name: Last name: function myFunction(){ var fname = document.getElementById("fname").value; if(fname > 40); window.location.href = "http://www.w3schools.com"; else alert("Condition xxxxx met") }
Member 14916200 wrote:
if(fname > 40);
You have a syntax error in your script. If you check your browser's developer console, you should see an error logged telling you that the
else
is not expected. That's because you've got an extra semicolon at the end of yourif
line, which turns your code into:if (fname > 40) {
// Do nothing
}// Outside of the "if" block now...
window.location.href = "...";// Invalid "else" statement:
else {
alert("...");
}Remove the extra semicolon and your code should work. You should also put braces around the branches:
if (fname > 40) {
window.location.href = "...";
}
else {
alert("...");
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I have the following code and if the condition is true it should load a web page but it dont. Help would be appreciated: Enter your aggretate for your Matric final exam First name: Last name: function myFunction(){ var fname = document.getElementById("fname").value; if(fname > 40); window.location.href = "http://www.w3schools.com"; else alert("Condition xxxxx met") }
Hi there, As pointed by Richard Deem, there are many syntax errors. Editing your code as below should work.
function myFunction(){
var fname = document.getElementById("fname").value;if(fname > 40)
{window.location.href = "http://www.w3schools.com";
}
else {
alert("Condition xxxxx met");}
}