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.
- Is it good to stop the execution of javascript abruptly.
- Is it bad if the functions are not returned to the calling function and finally to the operating system.
- 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.