Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Confirm close of browser window. (IE5)

Posted on 2002-07-19
5
Medium Priority
?
817 Views
Last Modified: 2012-05-04
I only need an IE5 solution to this if it makes it easier. (love homogenous intranet environments)

I'd like a script similiar to the following:

--start-code--

<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--

function window_onbeforeunload() {
     return false;

}

//-->
</SCRIPT>
</HEAD>
<BODY LANGUAGE=javascript onbeforeunload="return window_onbeforeunload()">

<P>&nbsp;</P>

</BODY>
</HTML>


--end-code--


What this code attempts to do, is ask a user to confirm yes/no before they close the browser window. But the message is a little ugly.

The dialog is:
"Are you sure you want to navigate away from this page?

false

Press OK to Continue or Cancel to stay on the current page"


Now, what I am trying to do, is ask the user if they want to save their changes (to an HTML form) before closing the window. But I need a cleaner dialog than the one I am getting. (It is just going to confuse people)

Ideally the logic would be this...

User clicks to close window.
Dialog comes up asking user if they'd like to save changes.
If user clicks no, allow window to close.
If user clicks yes, cancel the window_close and call function saveChanges();

Can it be done? I don't mind using an HTA (HTML application) if it allows me to do it.
Cheers,
Dave.
0
Comment
Question by:davebeer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 22

Accepted Solution

by:
CJ_S earned 240 total points
ID: 7164402
Using HTA and in the frameset or body:

onbeforeunload="if(confirm('would you like to save first?')){ saveChanges(); return false; }"
0
 

Author Comment

by:davebeer
ID: 7198302
Sorry, I'd thought I'd already responded to this.

Your code fragment works for the case of not wanting to save any changes.
But if the user selects 'Yes', the SaveChanges function runs, and then the user sees the message:

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

false

Press OK to Continue or Cancel to stay on the current page"
...


It is that message that I am trying to prevent.
0
 

Author Comment

by:davebeer
ID: 7198310
Actually.. I sorted it. I just needed to change the onbeforeunload event to onunload... It seems to do the trick. :-)
0
 

Expert Comment

by:impeeza
ID: 24930021
Hello Dave, i didn't understand what do you do to avoid the message?
0
 

Author Comment

by:davebeer
ID: 24978103
Well, it was 7 years ago. :-)
Think I did what it says on the tin.

put CJ's code into the onunload event. That way, it fired when the page was unloading. From what I remember it worked well.
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question