Javascript + onbeforeunload + popup on page exit

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>
dkilbyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SnowFlakeCommented:
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
SnowFlakeCommented:
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
Michel PlungjanIT ExpertCommented:
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>

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

dkilbyAuthor 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>
dkilbyAuthor Commented:
Even if i change isRedirecting to isRedirecting=true; the warning still popups
Michel PlungjanIT ExpertCommented:
try
 event.returnValue = (isRedirecting)? null:"Please save first";
dkilbyAuthor 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 ExpertCommented:
try


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

<body onBeforeUnload="closeIt()">
SnowFlakeCommented:
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 ExpertCommented:
It is the modification of event.returnValue in the onBeforeUnload that triggers the popup, not the return value in the event itself
dkilbyAuthor 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.

            
SnowFlakeCommented:
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
dkilbyAuthor 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.
SnowFlakeCommented:
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.
dkilbyAuthor 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.
SnowFlakeCommented:
Your welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.