未捕获的范围错误:加载时超过了最大调用堆栈大小

Uncaught RangeError: Maximum call stack size exceeded onload

本文关键字:过了 调用 堆栈 加载 范围 错误      更新时间:2023-09-26

我在上一页上有一个按钮,它会重定向到这个页面,但问题是当页面加载时,它不会显示确认框。我收到这个错误

Uncaught RangeError: Maximum call stack size exceeded

页面代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
//script
<script type="text/javascript">
    function confirm(){
        var con = confirm("Are You Sure?");
            if(con = true){
                window.location = "delete.php";
            }
        else{
            history.go(-1);
    }
        }
</script>
<body onload="confirm()">
</body>
</html>

您的javascript代码中有两个问题:

  1. 您已将函数命名为与要调用的保留函数相同的函数
  2. 你的比较实际上是一种分配

要解决第一个问题,只需将函数的名称更改为其他名称,如confirmDeletion()

<script>function confirmDeletion() { /* do stuff */ }</script>
<body onload="confirmDeletion()">

要解决第二个问题,请更改比较。在javascript中,if语句会自动将输入强制为布尔值,这意味着您实际上不需要将其与true进行比较。

if (con) {
    /* do confirmed true stuff */
} else {
    /* do confirmed false stuff */
}

为了将来参考,请确保始终使用三重相等的===符号进行比较,否则会出现意外行为。

您总是要返回1页,因为您没有正确评估您的状况。

if (con = true) {
   window.location = "delete.php";
}

应该是

if (con == true) {
  window.location = "delete.php";
}

注意附加的==是赋值运算符,==用于比较和评估条件。

尝试将函数从confirm重命名为其他函数。问题是,通过在confirm函数中调用confirm,您将进入一个无限循环。

例如,这将在我将confirm重命名为myConfirm:时起作用

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
//script
<script type="text/javascript">
function myConfirm(){
    var con = confirm("Are You Sure?");
        if(con = true){
            window.location = "delete.php";
        }
    else{
        history.go(-1);
}
    }
</script>
<body onload="myConfirm()">
</body>

编辑

还要将con = true更改为con == true,以检查con是否为真,而不是为其指定值true

更改函数的名称。条件if(con = true)将truthy值分配给con您应该比较像if(con)这样的truthy。你应该这么做,

<script type="text/javascript">
    function confirmNavigation(){
        var con = confirm("Are You Sure?");
            if(con){
                window.location = "delete.php";
            }
        else{
            history.go(-1);
    }
        }
</script>