www.webdeveloper.com
Results 1 to 3 of 3

Thread: Throwing an error to abort javascript execution

  1. #1
    Join Date
    Nov 2005
    Location
    chennai, tamil nadu, india
    Posts
    77

    Throwing an error to abort javascript execution

    Hi,
    I call a javascript function b from another function a.
    in the function b at certain conditions i want to stop the javascript execution and i have did it by throwing an exception by using Throw New Error('Stopi').
    It is working well.
    1. Is it good to stop the execution of javascript abruptly.
    2. Is it bad if the functions are not returned to the calling function and finally to the operating system.
    3. I will there be a over flow of stack or will stack keep increasing if i am throwing many such errors.


    Meanwhile IE shows the yellow exclamation icon in its status bar as usual that there is a javascript exception. When you click that you can see the error description as 'Stopi' which i have thrown.

    Here is an example...

    <input type='text' name='name' id='name' />

    Code:
    function validate()
    {
          isempty('name','Please enter your name');
         // no if condition or return statement here
    }
    
    function isempty(src,msg)
    {
         var so = document.getElementById(src)
         if(so.value=='')
         {
              alert(msg)
              so.focus()
              throw new Error('Stopi')
          }
    }
    so i dont have to use if condition when validating. no braces, no return statement, and no need to call focus for every if condition so this code reduces 6 lines and multiplied by number of input controls you are going validate. so i just need to call isempty and if the control has empty value then the user will get an alert and the execution stops.

    Please suggest.

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Could be:
    Code:
    function validate()
    {
      return isempty('name','Please enter your name');
    }
    function isempty(src,msg)
    {
         var so = document.getElementById(src)
         if(so.value=='')
         {
              alert(msg);
              so.focus();
              return false;
          }
    return true;
    }
    if the validate function is called like that:
    Code:
    <form action="" onsubmit="return validate()">

  3. #3
    Join Date
    Nov 2005
    Location
    chennai, tamil nadu, india
    Posts
    77

    that is fine... anyway

    it is not that i have just one validating rule ... but i have more than 1 and it is not that i am using this code just for this purpose... i am using it in various places... just want to know its disadvantages.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles