Y
YouSui
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>
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>