views:

988

answers:

5

Hello everyone,

Any javascript to prompt a message box when a user closes IE? I have tried to find a code sample for quite a while but failed.

thanks in advance, George

Here is my html code, but it has the following error. Any ideas?

To help protect your security, Internet Explorer has restricted this webpage from running scripts or ActiveX controls that could access your computer. Click here for options...

+6  A: 

You are looking for the beforeunload event.

For example:

function someCloseEvent() {
  return "Any string value here forces a dialog box to \n" +
         "appear before closing the window.";
}

window.onbeforeunload = someCloseEvent;
Aron Rotteveel
I used your code but has the following security error, any ideas?To help protect your security, Internel Explorer has restricted this webpage from running scripts or ActiveX controls that could access your computer. Click here for options...(I post my html code in question.)
George2
My code.<html><body><script language="javascript"> <!--function someCloseEvent() { return "Any string value here forces a dialog box to \n" + "appear before closing the window.";}window.onbeforeunload = someCloseEvent;//--></script>Close me!</body></html>
George2
@George2: You are running the file from the local computer. As a security measure, browsers don't execute scripts on local files. Either add localhost to the trusted ring, or put the file on a server.
voyager
+1  A: 

or the onUnload? -> http://www.w3schools.com/jsref/jsref_onunload.asp

Jasper
I have tried your code, but there is a security warning. Any ideas to remove it? To help protect your security, Internel Explorer has restricted this webpage from running scripts or ActiveX controls that could access your computer. Click here for options...
George2
if you are using localhost-testing, that is a common thing, just activate it ;-)
Jasper
I see. I have tested on another IIS server and you are correct. Any ideas why there is such error on localhost testing? Localhost should be more trustable than from remote IIS server.
George2
Lol, it isn't actually an 'error', it is one of those Internet Explorer related issues. I'm pretty sure somewhere you can tell Ie to trust localhost, or else try clicking on it and enabling the activeX component - it'll probably work after that
Jasper
I am confused why it is mentioned I am executing ActiveX component? I am just executing some javascript. :-)
George2
I can't tell you 'why' Internet Explorer does this. I googled it a bit, can't find why but I did find that it normally only happens when you open the file locally. I suggest either using a localhost or a different browser. Firefox has excellent tools for javascript debugging.
Jasper
+1  A: 

You will get unload message during refresh also. Better to check clientx and clientY also in the beforeunload. This is a pseudo code...

<script type="text/javascript">

 var myclose = false;

 function ConfirmClose()
 {
  if (event.clientY < 0)
  {
   event.returnValue = 'Any message you want';

   setTimeout('myclose=false',100);
   myclose=true;
  }
 }

 function HandleOnClose()
 {
  if (myclose==true) alert("Window is closed");
 }
</script> onbeforeunload="ConfirmClose()" onunload="HandleOnClose()"
lakshmanaraj
I have tried your code, but there is a security warning. Any ideas to remove it? To help protect your security, Internel Explorer has restricted this webpage from running scripts or ActiveX controls that could access your computer. Click here for options...
George2
This is IE's specific behaviour.You may need to add "Trusted sites" as your site or may decrease the level of security to allow Scripts to run in the browser.
lakshmanaraj
A: 

there isn´t a good form to do that,

try this codes above, and press alt+f4.

Sorry, code above you mean which code snippet?
George2
+1  A: 

On a side note, I would seriously consider whether you want to do this. When I close the window, I'm done with your site, I don't want any more messages from you. Popping up an extra window is going to annoy a lot of people.

Kevin
I am dealing with some important information when showing this message to ensure user does not wrongly click close button. :-)
George2
When tested locally, there is a security warning like this, but on remote IIS server it is ok. Any ideas to remove it? To help protect your security, Internel Explorer has restricted this webpage from running scripts or ActiveX controls that could access your computer. Click here for options...
George2
on your local environment (e.g. localhost or similar) IE puts that bar up on any page. If you add the "MOTW" Mark Of The Web, IE will stop messing around. As you noted, on the Intranet or Internet it is fine.
scunliffe
MOTW: http://msdn.microsoft.com/en-us/library/ms537628(VS.85).aspx
scunliffe