-1

I've got this function function

 checkpassword() 
{
var password = document.getElementById("passwordBox");
var passwordtext = password.value;
if(passwordtext == "reece")
{
return true;
}
alert("acsess denied!!");
                                   ← ← ← ← ←
return false;
}

and i want to add a web page address here if it returns false Can you help?

  • 1
    So you want to redirect the user to a new page if the password is wrong? Set `window.location.href`. – JSON Derulo Sep 18 '17 at 18:17
  • Possible duplicate of [How to redirect to another webpage?](https://stackoverflow.com/questions/503093/how-to-redirect-to-another-webpage) – Sebastian Simon Sep 18 '17 at 18:30

2 Answers2

0

To redirect a user to a new page, set the window.location.href attribute.

window.location.href = 'yourPage.html';
JSON Derulo
  • 9,780
  • 7
  • 39
  • 56
-1

You cannot have two return statement for a function. Function execution will stop as it returns something. In this case in the else part you cannot return web page address and false independently. Have a data structure such as an object or an array having false and web addresses and return that structure.

Akash Verma
  • 344
  • 3
  • 12
  • You can absolutely have multiple `return` statements, if every `return` before the last one is not unconditional. You can still redirect to a different page and return something after that. – Sebastian Simon Sep 18 '17 at 18:29
  • You can have multiple paths of execution in a function where each path can have a return statement. Once that return statement is executed the control will go to the point where the function was called. Could you please elaborate on multiple return statement in the same path of execution? – Akash Verma Sep 18 '17 at 18:42
  • There _are_ two possible paths of execution in the OP’s code. – Sebastian Simon Sep 18 '17 at 18:59
  • Exactly and in my answer I only meant the else part of execution. – Akash Verma Sep 18 '17 at 19:01
  • I wrote the else path can't have two return statement – Akash Verma Sep 18 '17 at 19:03
  • What are you talking about? There is no `else` part. If you mean the code after the `if`, there are no two `return` statements, there’s only one. – Sebastian Simon Sep 18 '17 at 19:04
  • Correct. What I meant is if he wants to send web address and false both to the caller in after if part he can't write two return statements in after if part of the code. – Akash Verma Sep 18 '17 at 19:10