3

I need to stop my script. I don't want to write more code like if(...) return false because I will use it many times.

My English is poor. It's my first question on this site. :)

3
  • Welcome to SO. What have you tried so far. Please post it here or please elaborate on your question. Commented Jan 9, 2013 at 6:18
  • It sounds like you want to just kill everything that's running without having to return from every function in the stack? I guess you could throw an exception, which will stop everything (unless the code is in a try ... catch block). That's very messy though, and it will appear as a JavaScript error in the user's browser. Commented Jan 9, 2013 at 6:19
  • document.write("string"); Commented Jan 9, 2013 at 6:19

2 Answers 2

4

you can simply write return false; to do so.

Sign up to request clarification or add additional context in comments.

1 Comment

OP has already told that he doesn't want to use return false;. (This is an example, how people answers / upvotes answers without reading the question properly. )
0

In order to exit ALL scopes of your script, you could throw an exception and catch it at the outer scope of your script ;) This imposes, that you adapt your exception handling.

Please note: It is better to write your program in such way, that the following is NOT necessary!

function ExitException() {}

try { //Do your script-stuff here

  Foo();

  //and do this at any place where you wish to exit:
  throw new ExitException;
}
catch (e) {
  if (e instanceof ExitException) //This is an Exit. All fine.
    console.log("Exit");
  else //Pass Exception to the Browser
    throw e;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.