web frontend - How to exit javascript script tags early? -
i have page bunch of .... sections. in 1 of them, half way through , decide want stop, , not run rest of contents of script tag - still run other code segments on page. there way without wrapping entire code segment in function call?
for example:
<script type='text/javascript'> console.log('1 start'); /* exit here */ console.log('1 end'); </script> <script type='text/javascript'> console.log('2 start'); console.log('2 end'); </script>
which should produce output
1 start 2 start 2 end
and not 1 end
.
the obvious answer wrap script in function:
<script type='text/javascript'> (function(){ console.log('1 start'); return; console.log('1 end'); })(); </script>
although best approach, there cases not suitable. question is, other way can done, if any? or if not, why not?
you can use break statement :
breakcode : { document.write('1 start'); /* exit here */ break breakcode; document.write('1 end'); }
with label reference, break statement can used jump out of code block
reference
Comments
Post a Comment