Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-10-2009, 06:37 AM   PM User | #1
jayapalchandran
New Coder

 
Join Date: Jan 2007
Location: Chennai, Tamil Nadu, India
Posts: 69
Thanks: 4
Thanked 0 Times in 0 Posts
jayapalchandran is an unknown quantity at this point
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/
- யானைக்கும் அடிசறுக்கும்
jayapalchandran is offline   Reply With Quote
Old 05-05-2011, 10:20 PM   PM User | #2
jayapalchandran
New Coder

 
Join Date: Jan 2007
Location: Chennai, Tamil Nadu, India
Posts: 69
Thanks: 4
Thanked 0 Times in 0 Posts
jayapalchandran is an unknown quantity at this point
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/
- யானைக்கும் அடிசறுக்கும்
jayapalchandran is offline   Reply With Quote
Old 05-06-2011, 09:43 AM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-06-2011, 10:28 AM   PM User | #4
jayapalchandran
New Coder

 
Join Date: Jan 2007
Location: Chennai, Tamil Nadu, India
Posts: 69
Thanks: 4
Thanked 0 Times in 0 Posts
jayapalchandran is an unknown quantity at this point
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/
- யானைக்கும் அடிசறுக்கும்
jayapalchandran is offline   Reply With Quote
Old 05-06-2011, 12:05 PM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-06-2011, 05:38 PM   PM User | #6
jayapalchandran
New Coder

 
Join Date: Jan 2007
Location: Chennai, Tamil Nadu, India
Posts: 69
Thanks: 4
Thanked 0 Times in 0 Posts
jayapalchandran is an unknown quantity at this point
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/
- யானைக்கும் அடிசறுக்கும்
jayapalchandran is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:14 AM.


Advertisement
Log in to turn off these ads.