admin管理员组

文章数量:1287775

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.

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.

Share Improve this question asked Aug 17, 2016 at 12:37 DimenticaDimentica 8052 gold badges13 silver badges32 bronze badges 2
  • 3 What about return? – AgataB Commented Aug 17, 2016 at 12:39
  • 1 ... and please don't use the ?: operator for statements. The ? Operator is used for expressions. { /* some code */; var lang=script.getAttribute('lang'); if (lang==null) return; if (strlen(lang)==0) return; /* more code */ } – ikrabbe Commented Aug 17, 2016 at 12:45
Add a ment  | 

4 Answers 4

Reset to default 6

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;
}

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

Use this to stop script

return false;

use this after your statement

return false;

本文标签: JavaScript stop script if a condition is not metStack Overflow