Dimentica
Dimentica

Reputation: 805

JavaScript stop script if a condition is not met

I have a javascript file to which I send a parameter

   <script lang="en" src="/test/load.js" ></script>

In the file I have script similar to this:

  ! function()
{ some code 
  var lag   = ( script.getAttribute( 'lang' ) == null  || script.getAttribute( 'lang' ) == '' ) ? exit : script.getAttribute( 'lang' );

The idea is that I do not want to execute the code after the one quoted above in case that parameter 'lang' is missing or is an empty string. How can I do that, I tried using

 exit

or

 break

but they do not work for me.

Upvotes: 2

Views: 12342

Answers (4)

Akshay
Akshay

Reputation: 805

use this after your statement

return false;

Upvotes: 1

JLRishe
JLRishe

Reputation: 101730

I think the main problem is that you're trying to use the conditional operator as a sloppy alternative to an if statement.

Just use an if statement, with return:

var lang   = script.getAttribute('lang');
if (!lang) {
    return;
}

Upvotes: 6

Use this to stop script

return false;

Upvotes: 1

Josh
Josh

Reputation: 4806

It's code within a function, correct? How about return?

Upvotes: 1

Related Questions