Interrupt or terminate Javascript execution

if(a.value==1 && b.value==2) { try{callFunc() }catch(e) {} } frm.submit(); 

Inside the function callFunc() , what do I need to write so that the execution stops completely? It should not execute frm.submit();

 function callFunc() { //stop execution here -- ensure it won't execute fm.submit() } 
+4
source share
7 answers

Better one

 function Abort() { throw new Error('This is not an error. This is just to abort javascript'); } 

than from any where this is called

 try { for(var i=0;i<10;i++) { if(i==5)Abort(); } } catch(e){} 

For you

 function callFunc() { //stop execution here Abort(); } //code from where you are going to call try { if(a.value==1 && b.value==2) { callFunc() } frm.submit(); } catch(e) {} 
+3
source

As you discovered, JavaScript interrupt almost always includes exceptions. If you really can't change the wrapper, you may have to resort to something more extreme. One (evil) way to kill a script is to convince the browser that it takes too long to run an endless loop:

 function callFunc() { //stop execution here var n = 1; while (n) { n += 1; } } 

Modern browsers will allow the user to kill the script after a while. Of course, this will make your site look broken, but it should give you the leverage you need to get the best API in place.

If the busy cycle is too extreme, you can replace the simple add-on with a plug-in sleep, or perhaps a synchronous network request, which takes a very long time, wrapped in its own try / catch protection network.

+1
source

I understand what you are trying to do. You do not want to kill the Javascript interpreter, you just want to prevent the form from being submitted.

HTML

 <form id="myForm"> … </form> 

Javascript

 // Setup… var frm = document.getElementById('myForm'), a = { value: 1 }, b = { value: 2 }; // Can change this code var callFunc = function() { // Throwing will do nothing here, since // exceptions are being caught in a try/catch // Instead, let overwrite the submit handler with one that // will cancel the form submission, then restore the old handler var oldSubmitHandler = frm.submit; var killHandler = function(e) { // Prevents the submission e.preventDefault(); // Restores the old handler frm.submit = oldSubmitHandler; }; frm.submit = killHandler; }; // Can't change any of this code if(a.value==1 && b.value==2) { try { callFunc() } catch(e) { } } // Want to stop this from happening frm.submit(); 

See in action: http://jsfiddle.net/3A7xC/

+1
source

Best of all is:

 if(a.value==1 && b.value==2) { try{ callFunc(); frm.submit(); } catch(e) { // stop execution } } 

If an exception is frm.submit(); in the callFunc function, the string frm.submit(); will not be executed. Instead, he will move on to the catch clause.

0
source

Lots of answers, one more for fun.

You can put the code in a function, try the throw block, and then return from the catch clause:

 function foo() { var a = {value:1}; var b = {value:2}; if(a.value==1 && b.value==2) { try { callFunc(); } catch(e) { alert(e.message); return; } } alert('error didn\'t stop me!'); } function callFunc() { throw new Error('This is an error.'); } 

Otherwise, you can set the flag in the catch block and test it immediately after that, before moving on. Or take one of the other answer options.

0
source

So, the inside of callFunc is the only thing you can change? How about this:

 callFunc(){ frm.submit(function() { alert('this should not submit'); return false; }); } 
0
source

You can abort javascript execution with throw :

 if(a.value==1 && b.value==2){ try{callFunc() }catch(e) {} } frm.submit(); function callFunc() { throw "stop execution"; } 
-1
source

Source: https://habr.com/ru/post/1403022/


All Articles