Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > What’s the best way to solve this kind of javascript dead loop?

Reply
Thread Tools

What’s the best way to solve this kind of javascript dead loop?

 
 
YouSui
Guest
Posts: n/a
 
      02-09-2010
Hi guys,

Recently I'm maintaining a legacy project. I found one javascript dead
loop problem in a page. The demo code is as follows. When the user
clicks the first inputbox and type in 3 then directly click the second
input box, the dead loop occurs. Now my question is what's the best
way to solve or prevent this kind of problem? Great thanks.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>

<input type="text" name="a" id="a" value="" />
<input type="text" name="b" id="b" value="" />

<script type="text/javascript" charset="utf-8">
var a = document.getElementById("a");
a.onblur = function(){
if(a.value.length === 1){
alert("aaa");
a.focus();
a.select();
return false;
}
}

var b = document.getElementById("b");
b.onblur = function(){
if(b.value < a.value){
alert("bbb");
b.focus();
b.select();
return false;
}
}
</script>
</body>
</html>

 
Reply With Quote
 
 
 
 
Matthias Reuter
Guest
Posts: n/a
 
      02-09-2010
YouSui wrote:

> Recently I'm maintaining a legacy project. I found one javascript dead
> loop problem in a page. The demo code is as follows. When the user
> clicks the first inputbox and type in 3 then directly click the second
> input box, the dead loop occurs. Now my question is what's the best
> way to solve or prevent this kind of problem? Great thanks.
>
> var a = document.getElementById("a");
> a.onblur = function(){
> if(a.value.length === 1){
> alert("aaa");
> a.focus();
> a.select();
> return false;
> }
> }
>
> var b = document.getElementById("b");
> b.onblur = function(){
> if(b.value < a.value){
> alert("bbb");
> b.focus();
> b.select();
> return false;
> }
> }


The best way to prevent this is to gracefully notify the user of mistakes.
Don't use an alert box but insert some text on the page. Then don't mess
with the user. If he focuses one element don't force the focus on a
different element.

I would recommend to validate user input on submit and not before. Imagine
in your example the user entered a valid value for a and some value for b,
which unfortunately is smaller than a. If now a is the wrong value and b
is correct (to the user), the only way to change the value of a is to
change b to a wrong value, then change a, and then change b again.

Matt
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
best way to check if pid is dead? bukzor Python 4 05-21-2008 10:36 PM
Another kind of ConcurrentModificationException, how can I solve it? Bill David Java 4 12-21-2007 07:20 AM
some OT: how to solve this kind of problem in our program? oyster Python 12 12-30-2006 04:24 PM
What's the best way to split this kind of string? Sam Kong Ruby 5 03-13-2006 05:43 PM
Struts with vectors? Best way to solve this problem? Scott Phelps Java 3 06-10-2005 02:42 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57