Confirm()?

I’m trying to use confirm() whe a page is loaded,

<script>
window.onload = function confirm() {
  let r = confirm("Creating a (48 Slot) rack!");
  if (r != true) {
		window.location.href = "../rooms/data.php";
  }
}
</script>
but am getting this error ![image](upload://2ieqB0MEpeyFTBBwSGeILxMtIb7.png) What does that mean where is the recursion?

Uploading the image seems to have failed. Could you try again please?

Use:

<script>
window.onload = function() {
  let r = confirm("Creating a (48 Slot) rack!");
  if (r != true) {
		window.location.href = "../rooms/data.php";
  }
}
</script>
1 Like

Here is the recursion:

The function is called confirm and it is calling confirm, i.e., itself.

3 Likes

It’s also bad practice to use onload directly, since other scripts you might include may also be trying to use the load event. I’d also suggest losing the “variable for nothing” and literal / strict comparison.

window.addEventListener('load', function() {
	if (!confirm("Creating a (48 Slot) rack!")) {
		window.location.href = "../rooms/data.php";
	}
});

This way multiple scripts that need to hook onload can get along, and the overall code is simplified.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.