分割 url 导致 Firefox 出现错误

发布于 2024-11-27 19:45:44 字数 2962 浏览 2 评论 0原文

我正在尝试使用 get 在公式之间实现变量的传递,并且我基本上使用 split() 方法来恢复每个页面上的变量。我遇到的问题是,当我实现拆分时,脚本停止了。

之前并没有这样做,现在我添加了第二个函数来检查所有输入名称的所有值,我遇到了这个问题。我是 javacsript 的新手,所以我真的不知道在哪里寻找,最重要的是我真的需要能够获取变量的值。 这是第一个表单和第二个表单的 html,其中 url.split("?"); 导致 Firefox 和我的计算机在此过程中迷失……

这是第一页:

<!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>
</html>


这里是part1.html页面,其中包含有错误的脚本:

<!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>

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>
</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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赢得她心 2024-12-04 19:45:44

您正在更改其他循环中的循环迭代器,导致无限循环。

将此行更改

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

为:

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

这样您就不会进入无限循环。

需要一些解释。在函数 subnewsc 中,您有循环,使用 i 作为循环迭代器。由于您之前没有 var,因此它正在成为全局变量。现在,在该循环中,您调用函数 gValue,其中也有循环,再次使用 i 作为循环迭代器,如果没有 var,则意味着使用与第一个循环中的相同变量。这当然造成了严重破坏。

例如,当您读取第二个查询字符串项的值时,在调用 var Score = gValue(schck); 后,i 的值为 1,因此它永远不会获得更多值比 3.

通过添加 var 关键字,您将使变量具有本地作用域并解决所有这些混乱。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文