Forum Moderators: open

Message Too Old, No Replies

Repeating confirm boxes

         

someone788

10:56 pm on May 9, 2006 (gmt 0)

10+ Year Member



Hello all, I have the following script:

<script>
<!--
var your_message = "Are you sure you want to leave?";
var times = 0;

function onul() {
if (times == 0) {
var leave = confirm(your_message);
if (!leave) location = self.location;
times++;
}
}
//-->
</script>

<body onunload="onul()">

When a user leaves a page, it asks if they really want to, and they have the option of selecting OK or cancel.

I'm wondering how to make it so when you press OK, it will just display the same alert again, and when cancel is selected it will stay on the page, not displaying the alert again unless they try to leave the page. Yes sort of like a trap, don't ask ;)

Thanks!

Fotiman

2:10 pm on May 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not that I approve of such behavior, but if you wanted to it would probably be as simple as:

<script>
<!--
var your_message = "Are you sure you want to leave?";

function onul() {
var leave = confirm(your_message);
if( leave ) onul();
location = self.location;
}
//-->
</script>

someone788

4:20 am on May 17, 2006 (gmt 0)

10+ Year Member



Hm, I should have probably mentioned that this was for a demonstration page where users know what is going to happen to them, but either way that script doesn't seem to be working.

kaled

10:42 am on May 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If a browser has been tested properly by its designers, this will be impossible. All that is required by the programmer is to set a flag when onunload is triggered that effectively disables functions such as changing the location. However, depending on how the basic code were written, it might not even be necessary.

Kaled.