开发者

splitting url causing firefox to bug

开发者 https://www.devze.com 2023-03-25 03:29 出处:网络
I am trying ot implement the passing of variables with get between formulars and I basically use the split() method to recover the variable on each page. The problem that I have is that the script is

I am trying ot implement the passing of variables with get between formulars and I basically use the split() method to recover the variable on each page. The problem that I have is that the script is stopped when I implement the splitting.

It wasn't doing so earlier and now that I added the second function to check all the values of all the input names, I get this problem. I am new to javacsript so I don't really know where to look for and on top of this I really need to be able to get the variable's value. Here are the html of the first form and of the second one, with the url.split("?"); causing firefox and my computer to get lost in the process...

Here the first page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title></title>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="robots" content="index, follow" />
<meta name="googlebot" content="index, follow" />
<script type="text/javascript">
<!--

// -->
</script>
</head>
<body>
<div>Choose between<br />
<form name="fo" method="get" action="part1.html">
<input type="radio" name="s1" value="1" />one<br />
<input type="radio" name="s2" value="2" />two<br />
<input type="radio" name="s3" value="3" />three<br />
<input type="submit" value="continuer" />
</form>
</div>
</body&g开发者_高级运维t;
</html>

here the part1.html page, that contains the buggy script:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<script type="text/javascript">
<!--
function gValue(varname) {
    var url = window.location.href;
    var qparts = url.split("?");

    if (qparts.length == 0) {
        return "";
    }

    var query = qparts[1];
    var vars = query.split("&");
    var value = "";

        for (i=0;i<vars.length;i++) {
            var parts = vars[i].split("=");
            if (parts[0] == varname) {
                value = parts[1];
                break;
            }
        }
    return value;
}

function subnewsc() {
    for(i=1;i<=3;i++) {
        var schck = "s" + i;
        var score = gValue(schck);
        score = parseInt(score);
        i = parseInt(i);
        var newscore = score+i;
        var doc = "document.fo.s" + i;
        doc.value=newscore;
    }
}
// -->
</script>
</head>
<body onload="subnewsc();">
<div>choose between<br />
<form name="fo" method="get" action="part2.html">
<input type="radio" name="s1" value="1" />one again<br />
<input type="radio" name="s2" value="2" />two again<br />
<input type="radio" name="s3" value="3" />three again<br />
<input type="submit" value="continuer" />
</form>
</div>
</body>
</html>


You are changing the loop iterator in other loop, causing infinite loop.

Change this line:

for (i=0;i<vars.length;i++) {

To this:

for (var i=0; i<vars.length; i++) {

And you won't get into infinite loop.

Some explanation is required.. in the function subnewsc you have loop, using i as the loop iterator. As you don't have var before, it's becoming global variable. Now inside that loop you call the function gValue where you also have loop, again using i as the loop iterator and without the var it means using the same variable as in the first loop. This of course is causing havoc.

For example, when you read the value of second querystring item, i will have value of 1 after you call var score = gValue(schck); so it will never get more than 3.

By adding the var keyword you'll make the variable have local scope and solve all this mess.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号