How do we abort JavaScript execution at any instance even inside a loop.
Create a function with a name jsexit or jsabort
and have this as its code
function javascript_abort() { throw new Error('This is not an error. This is just to abort javascript'); } |
instead of the error string which i have given you can give any string you want. This is really working well for me.
call it anywhere like javascript_abort()
for example
// assume that you have this code inside a nested function. for(var i=0;i<10;i++) { if(i==5)javascript_abort(); alert(i); } //so at any place it will stop the execution. |
But in IE browsers you will see an yellow exclamation to indicate that there is an error in the javascript.
Otherwise every thing is perfect i believe. In other browsers this will not appear. If you have enabled error reporting add-on for firefox then it would show a graphic notification. that too is not at all a problem.
If anybody (the users) clicks the yellow exclamation in IE they see this string as error message ‘This is not an error. This is just to abort javascript’.
But still i need an option to exit javascript execution without making IE or any other browsers that an error has occured when a new error has been thrown.
I would like to have comments, suggestion and objections about this because i would like to know what people think about this.
August 22nd, 2010 at 6:38 am
me ayudo mucho esta instruccion, para abortar, Muchas Gracias por la ayuda.
September 29th, 2010 at 10:50 pm
el placer es mío. disfrutar. (pleasure is mine. enjoy)
May 6th, 2011 at 2:48 pm
Have you considered using the simple command : return ?
May 6th, 2011 at 2:49 pm
And if you just want to stop the execution of a loop and jump off, you may simply use the command : break
May 6th, 2011 at 3:22 pm
what if your code were deep inside a nested function and you want to abort at that time.
This worked very well for me. Still i am trying other means through discussion forums and i will post it here when i found one.
Still you can reply if you feel the above is not needed.
February 10th, 2012 at 4:52 pm
Why not catch the error at a higher level?
And if you do not want IE to show anything catch it at the highest level – the entry point.
try {
// Your entry point…
} catch(e) {
// Handle error message if appropriate
alert(e.message);
}
November 6th, 2012 at 4:02 pm
Nei controlli a cascata la tua soluzione è formidabile; grazie. Prende un controllo superiore a return e anche a focus: l’ho trovata unica, semplice, efficace, efficiente e economica.