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
4 Answers
Reset to default 6I 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
版权声明:本文标题:JavaScript stop script if a condition is not met - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741244384a2364594.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论