forcing javascript to abort - stop javascript execution at any time

JavaScript Add comments

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.

7 Responses to “forcing javascript to abort - stop javascript execution at any time”

  1. glimachi Says:

    me ayudo mucho esta instruccion, para abortar, Muchas Gracias por la ayuda.

  2. Admin Says:

    el placer es mío. disfrutar. (pleasure is mine. enjoy)

  3. Kor Says:

    Have you considered using the simple command : return ?

  4. Kor Says:

    And if you just want to stop the execution of a loop and jump off, you may simply use the command : break

  5. admin Says:

    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.

  6. Roger Says:

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

  7. Roberto Says:

    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.

Leave a Reply

Entries RSS