Jamesking56
Jamesking56

Reputation: 3901

PHP's exit; in Javascript?

What is the equivalent of PHP's exit; in Javascript/jQuery?

I need to stop my script early depending on certain conditions... The only answers I could find from search was stopping forms from submitting...

Upvotes: 9

Views: 11963

Answers (4)

Jovylle
Jovylle

Reputation: 961

From looking on your certain problem, and for everyone else. Please see this. This MIGHT help you.

Just put this one line

if(exit_condition!=true)

and also this requires your codes to be inside of

$(document).ready()

if(exit_condition!=true)
    $(document).ready(function() {
      // Your code
      // Your code
      // Your code
      // Your code
    });

I have also encountered this issue, @Jamesking56 has encountered. And this is my solution.

Upvotes: 1

arutaku
arutaku

Reputation: 6097

You could try:

throw "stop execution";

Using return you will skip the current function, that's why throwing is more similar to PHP exit();

Upvotes: 15

Florian Bauer
Florian Bauer

Reputation: 636

I would finish a function early with a return statement:

return;

Upvotes: 3

GolezTrol
GolezTrol

Reputation: 116110

A javascript function is terminated by calling return. Usually you will be in the context of a function or something. Also event handlers (like for onclick on an element) will behave like a function and accept return.

Upvotes: 2

Related Questions