Hi. I was wondering if there was a command that allows you to return to the beginning of the script.

Let's say

/pseudocode/

You ask for pass word

if password is correct --> do nothing or just display a random message

else

if password is incorrect or no password is entered --> return to the beginning of the script to ask for the password again


/pseudocode/

I wanna know if there is a way to do this without having to ask for the password and writing the if else statements again and again.

Oh and another question. Is there a way to call a html file while in javascript? I know it is possible to call javascript file while in html but I wasn't sure about the other way around. thanks in advanced.

Recommended Answers

All 3 Replies

Maybe something like:

/pseudocode/

var pwdEntered = false;

while (pwdEntered == false){

   //CODE here to ask user for password
   ...
   ...
   var usrEnteredPwd = [I]some user entered string[/I];

   //CODE here to verify user entered password
   if ((userEnteredPwd != "") AND (userEnteredPwd != null) AND (userEnteredPwd == [I]correctPwd[/I]){

      pwdEntered = true; //this statement breaks the while loop after user entered correct pwd

   }
}

//CODE here for progression of logic after user has entered correct password.

/pseudocode/

Hope this helps.

Hi Bookmark,

You can use loop statements such as 'for' or 'while' to return execution to the beginning of a block of code. You'll also need an exit condition, otherwise you'll end up in an infinite loop. In your case the exit condition should be a password match.

Return commands are something different. These come into play when you want to return a value from a function or exit a function at a specific point. When a function returns, execution resumes with the statement following your function call. A return command will not take you to the top of a script.

Writing JavaScript to check a password can be trivial, and here's a minimal example:

while(prompt("Enter the password:") != "password");

However, JavaScript based password routines are insecure and rarely a good idea. This is because the source code is publically accessible. There is little to prevent anyone from reading your password or bypassing the check altogether.

For more secure forms of password checking, you might want to look at server-side scripting languages, possibly PHP or ASP.NET.

To answer your second question, HTML is not executable code, so cannot be called. JavaScript will load XML, but you'll find this topic is beyond the basics. Post another question when you get there ;-)

Hey thanks guys for the replies!! This will definitely help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.