views:

111

answers:

3

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"&gt;
<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>
A: 

What do u mean by this line:

if(b.value < a.value){

If you are comparing length, do this:

if(b.value.length < a.value.length){

Or if you want to check if they are equal or not then:

if(b.value == a.value){

Or

if(b.value != a.value){
Sarfraz
Hi Sarfraz, **if(b.value < a.value){** is just a demo condition. I just want to say that there is some condition there.
Yousui
what exactly do u want to do?
Sarfraz
+1  A: 

Try this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<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() {
    a.validCheck = false;
    if(a.value.length === 1){
        alert("aaa");
        a.focus();
        a.select();
        return false;
    }
    a.validCheck = true;
}

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

Plus I didn't have any loop problems in Firefox although it doesn't behave as intended. I'm assuming this is IE only.

Watts
+1  A: 

This works in IE.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>

<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-type" />
</head>

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

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

var bInBlurB = false;
var b = document.getElementById("b");
b.onblur = function(){
    if (bInBlurA) return;
    if(b.value < a.value){
        bInBlurB = true;
        alert("bbb2");
        b.focus();
        b.select();
    }
}
a.onfocus  = function()
{
    bInBlurA = false;
}
b.onfocus = function()
{
    bInBlurB = false;
}
    </script>

</body>
</html>
Doug D