Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2007
    Location
    Chennai, Tamil Nadu, India
    Posts
    71
    Thanks
    5
    Thanked 0 Times in 0 Posts

    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.
    http://vikku.info/
    - யானைக்கும் அடிசறுக்கும்

  • #2
    New Coder
    Join Date
    Jan 2007
    Location
    Chennai, Tamil Nadu, India
    Posts
    71
    Thanks
    5
    Thanked 0 Times in 0 Posts

    and finally i have made up my mind

    Here is the one i am using like the above.

    http://vikku.info/codesnippets/javas...n-at-any-time/
    http://vikku.info/
    - யானைக்கும் அடிசறுக்கும்

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    My Goodness! You have returned after two years to post the solution? You have worked two years for that? I appreciate that...

    By the way, I guess you could have simply used a return command to stop the execution of a code
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Jan 2007
    Location
    Chennai, Tamil Nadu, India
    Posts
    71
    Thanks
    5
    Thanked 0 Times in 0 Posts

    not like that

    I thought of revamping this concept but still in the same place.

    Besides you said to use return. That is not the case where i want a code.

    I want javascript to terminate its execution at any line of code or at any moment.

    This will not disable javascript but will stop the execution for that instance.

    So after you do another event it will resume its work.

    I have explained in my code that the situation like be even in side a loop or inside a deep nested function.

    For those cases this code will be really helpful.
    http://vikku.info/
    - யானைக்கும் அடிசறுக்கும்

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    return does exactly what you want. It stops the code at any line, at any moment. There is also break, to stop the execution of a loop and jump outside.

    I still don't understand what you want. Maybe you could post a practical example, or something like this.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New Coder
    Join Date
    Jan 2007
    Location
    Chennai, Tamil Nadu, India
    Posts
    71
    Thanks
    5
    Thanked 0 Times in 0 Posts

    but...

    Sorry that i haven't posted the code that would explain what i want. I will do it in a few days. Thank you for your response. i will be back here.
    http://vikku.info/
    - யானைக்கும் அடிசறுக்கும்


  •  

    Posting Permissions

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