Dimentica Dimentica - 3 months ago 12
Javascript Question

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




but they do not work for me.


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) {