Gitesh Khurani
Gitesh Khurani

Reputation: 23

Uncaught RangeError: Maximum call stack size exceeded onload

I have a button on previous page which redirects to this page but the problem is when the page loads it doesn't show the confirm box. I get this error

Uncaught RangeError: Maximum call stack size exceeded

The code for the page:

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

Upvotes: 2

Views: 1569

Answers (4)

Sudhansu Choudhary
Sudhansu Choudhary

Reputation: 3360

Change the name of the function. The condition, if(con = true)is assigning truthy value to con You should be comparing for truthy value like if(con). You should be doing,

<script type="text/javascript">

    function confirmNavigation(){

        var con = confirm("Are You Sure?");

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

            }

        else{

            history.go(-1);

    }
        }

</script>

Upvotes: 0

Strikeskids
Strikeskids

Reputation: 4052

You have two issues in your javascript code:

  1. You have named your function to be the same as the reserved function you are trying to call.
  2. Your comparison is actually an assign

To fix the first problem, simply change the name of your function to something else like confirmDeletion()

<script>function confirmDeletion() { /* do stuff */ }</script>

<body onload="confirmDeletion()">

To fix the second problem, change the comparison. In javascript, the if statement automatically coerces the input into a boolean, meaning you don't actually need to compare it to true.

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

For future reference, make sure to always use triple equal === sign for comparison, otherwise you will get unexpected behavior.

Upvotes: 2

winhowes
winhowes

Reputation: 8065

Try renaming your function from confirm to something else. The problem is that you're going into an infinite loop by calling confirm inside your confirm function.

So for example, this would work as I've renamed confirm to 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>

Edit

Also change con = true to con == true to check if con is true rather than assigning it the value true.

Upvotes: 0

Darren
Darren

Reputation: 70728

You're always going to go back 1 page because you are not evaluating your condition correctly.

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

Should be

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

Note the additional =, = is an assignment operator and == is used to compare and evaluate the condition.

Upvotes: 1

Related Questions