Javascript + onbeforeunload + popup on page exit

dkilby
dkilby used Ask the Experts™
on
I am using the below code to warn the user if they try and leave the page, the thing is i have it on a page with a flash file, what i want is to be able to re direct to another page without the popup coming up.  Is there anyway for the popup to come up except if the page is being redirected to a certain page.

<HTML>
<HEAD>
<SCRIPT>
function closeIt()
  {
    event.returnValue = "";
  }
</SCRIPT>
</HEAD>
<BODY onbeforeunload="closeIt()">
   
</BODY>
</HTML>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
how will that redirection be made ?
If made by script if can set up a flag that says "DontWarn"

<HTML>
<HEAD>
<SCRIPT>
var WarnUser=true;

function closeIt()
  {
   if (WarnUser) {
     // Do whatevver
   }

function NavWitoutWarning()
  {
   WarnUser=false;
   location.href=....
   }

</SCRIPT>
</HEAD>
<BODY onload="WarnUser=true;" onbeforeunload="closeIt()">
   
</BODY>
</HTML>


SnowFlake
the reason I added the WarnUser=true in the onload is because I have found that in certain
scenario's code that is just floating in the head does not get executed upon refresh of the page.

SnowFlake
IT Expert
Top Expert 2009
Commented:
in Flash:
getURL('javascript:setRedirectBrowser()')


in js:

<HTML>
<HEAD>
<SCRIPT>
isRedirecting=false;
function setRedirectBrowser() {
  isRedirecting=true;
}

function closeIt() {
  event.returnValue = (isRedirecting)? "":"Please save first";

}
</SCRIPT>
</HEAD>
<BODY onbeforeunload="closeIt()">
   
</BODY>
</HTML>
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I tried putting the getURL code in Flash but it didn't seem to work, I am wondering if it is because the redirect is in Flash as well, but i need to post some variables in Flash to another page.

In Flash

      getURL('javascript:setRedirectBrowser()')
      getURL("data_transfer.aspx", "_self", "POST");

In JS

<SCRIPT language=javascript>
            
     isRedirecting=false;
            
     function setRedirectBrowser() {
          isRedirecting=true;
     }

      function closeIt() {
           event.returnValue = (isRedirecting)? "":"Please save first";
      }
</SCRIPT>

Author

Commented:
Even if i change isRedirecting to isRedirecting=true; the warning still popups
Michel PlungjanIT Expert
Top Expert 2009

Commented:
try
 event.returnValue = (isRedirecting)? null:"Please save first";

Author

Commented:
<SCRIPT language=javascript>
            
isRedirecting=true;
      
function setRedirectBrowser() {
isRedirecting=true;
}

function closeIt() {
event.returnValue = (isRedirecting)? null:"Please save first";
}
</SCRIPT>

and i get the popup window with

"Are you sure you want to navigate away from this page?"

null

"Press OK to continue, or Cancel to stay on the current page."
Michel PlungjanIT Expert
Top Expert 2009

Commented:
try


<SCRIPT language=javascript>
     function setRedirectBrowser() {
          window.onbeforeunload=null;
     }
      function closeIt() {
           event.returnValue = "Please save first";
      }
</SCRIPT>

<body onBeforeUnload="closeIt()">
try adding a return
<body onBeforeUnload="return closeIt();">

change
      function closeIt() {
           event.returnValue = "Please save first";
      }

to
       function closeIt() {
           if (isRedirecting) {
                return 'Please save first';
           }
      }


also you might wat to add an alert

       function closeIt() {
           alert(isRedirecting);
           if (isRedirecting) {
                return 'Please save first';
           }
      }

to check if isRedirecting has the right value.

SnowFlake
Michel PlungjanIT Expert
Top Expert 2009

Commented:
It is the modification of event.returnValue in the onBeforeUnload that triggers the popup, not the return value in the event itself

Author

Commented:
ok, i have this working partially, but i am having problems with the communication from Flash to the page, it seems that the when Flash calls the function it does the closeIT function 1st, so the warning window comes up 1st.

here is the code that works when testing on a page with a simple button calling setRedirectBrowser, if i press the button then close the browser it works fine.

<SCRIPT language=javascript>
            
isRedirecting = true;
            
function setRedirectBrowser() {
      alert("TEST")
      isRedirecting=false;
}
                  
function closeIt() {
      alert(isRedirecting);
      if (isRedirecting) {
      return 'Please save first';
      }
}
</SCRIPT>

I am using this in Flash to call the function

getURL("javascript:setRedirectBrowser()");

which as i said does call the function, but the closeIt function gets called 1st.

            
what if you will remove the
      getURL("data_transfer.aspx", "_self", "POST");
from the flash ?

will you then get the TEST alert ?

maybe you should open a Q on the http://www.experts-exchange.com/Web/WebDevSoftware/Flash/  TA ?
about calling a javascript function on the hosing page ??

SnowFlake

Author

Commented:
i did remove getURL("data_transfer.aspx", "_self", "POST"); and i only get the "TEST" alert after the warning popup, so afte the closeIT function runs.
hmm...
obviously :)
It actually makes sense the way used here to call the javascript function is by navigating to the javascript moniker (javascript:).
this means that you are leaving the original page so the onBeforeUnload function gets called.

assuming there is no other way to call javascript from actionscript maybe what you are looking for can be accomplished by going the othre way around.

i.e. so that when the page closes it should ask the flash if it should or should not show the message.

so that in the flash you should raise the "isRedirecting" flag when it is a redirect you are performing
and in the closeit function you will ask the flash if it know if he is currently performing a redirect.

The Q is how do you call actionscript from javascript ?
maybe this:

http://weblogs.macromedia.com/flashjavascript/

can be a good place to start.

SnowFlake.

Author

Commented:
ok found something that works.

function closeIT(){
     if (event.clientY < 0)
         event.returnValue = 'Are you sure you want to Quit?'
   }
}

this does what i need, if the user trys closing the browser using the 'X' the warning comes, but at the end of the flash movie the page is able to redirect.

Thanks for the help, i will split the points as you both where a lot of help.
Your welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial