You declared the variable lastText globally and used the value as u specified above. It was amazing. I will try it out in my project also. Thanks for the idea
Regards
Ramanan Kalirajan
Don't do it, it's a very, very bad idea!
Most companies have a coding standard that says: don't use global variables!!!
Just imagine all the things that can go wrong and how unreadable the code becomes.
For example:
a) somebody has the same idea and also declares a global variable lastText that he needs for his function that should show the last error message. He works on a different part of javascript (or js-file) than you, but on your current page, his and your code are merged. So lastText is a string now (the error message) and not an object anymore! All your functions that were using "lastText" start crashing. And even if you verify right before, it still might crash!
example:
your code:
- ...
-
<input type="text" id="myText1" onblur="lastText=this">
-
...
-
var lastText;
-
function doThis()
-
{
-
if (typeof(lastText) == "string") return; // test if it is an object or an error message
-
alert(lastText.id);
-
}
his code:
- setInterval("errorTest", 1000);
-
function errorTest()
-
{
-
var errorHappened = ... // test if an error happened or not
-
if (errorHappened) lastText="An error happened!";
-
}
Why is it crashing???
the thread errorTest is executed every second (see setInterval()) as a background thread.
Now somebody click on the textbox. if you are lucky, everything goes well. But It can also crash:
Let's say the foreground thread that executes your onblur() just stopped after executing line 7 of your code (right before the alert() message. It was interrupted by the background thread that tests for errors. This background thread now executes line 5 in his code and sets lastText to an error string. Now it is finished and your foreground thread that was interruped continues. It executes line 8 of your code and crashes, because lastText is now changed from an object to a string, therefore lastText.id does not exist!
These types of errors are very hard to debug:
your code alone works all well, his code alone works all well, but if both comes together on one page, it crashes sometimes. Because it's not always but only sometimes, you can't debug the code in a debugger. This error is very, very hard to catch! And all because you and he did not listen to the golden rule "Don't use global variables"!!! It all gets worse when you and him are from different companies! Whose fault is it, who should spend money to fix the code and retest it? A manager nightmare also.
b) readability:
You could write thousands of lines of codes in different js-files and includes HTML-pages (framesets) that make up your current webpage.
Now suddenly you see a function:
function showErrorMessage()
{
alert(lastText);
}
So where is lastText defined? What initial value does it have? How can I make sure that it is always defined when somebody enters the function?. In which of the different HTML and js files is it used? Ok, you can search for it with an editor, but it will also show you all the source code like "onBlur=lastText=this" and so on which have nothing to do with the error.
Your task of rewriting/extending the error messages becomes a nightmare, because you must understand the whole source code to know which line you should leave (because it deals with textboxes) and which line to change (because it deals with your error message). Can you understand thousands of lines of sourcecode withing 10 minutes, most of them you have not written yourself???. no? Me neither! I would need days to do it.
So stick to the golden rule "no global variable" and you (or somebody else) could have done the needed changes withing 10 minutes.