Link to home
Start Free TrialLog in
Avatar of Dave6969
Dave6969

asked on

Confirm on window close *only* and minimizing a window

Hi... this is only my second question on this site so I hope I'm doing all the right things!

I've built an application to help people fill out timesheets on a corporate intranet. It works fine although there are two finishing touches I would like to add to make it usable by staff generally. I can't find any way to do them so I thought I'd ask the experts!

Basically a shortcut will be added to the Startup folder in windows to go to the online application in IE (IE6 is the corporate standard so no avoiding that. IE7 may be introduced next year). After that I want the window to automatically minimize itself so it just sits on the taskbar -- I can't work out how to do that. I have some javascript which refreshes that page (which will remain minimized) on every quarter hour, presenting a popup asking you what you're doing. The minimized window should be accessible as people may want to update their tasks outside the quarter hour segments -- so minimizing wouldn't be just a case of changing the window coordinates and letting it sit off-screen.

Now comes the *really* tricky bit. I want to have it so that the window presents a confirmation message when that minimized window is closed, to prevent someone closing the window accidentally. Because that minimized window refreshes itself periodically and spawns a popup, none of the usual ways of doing this will work -- they kick in when you navigate away from a page (close or click on a link or generate popup etc). I want a confirm dialogue to appear ONLY when the window is closed and at no other time.

If it's any help the application does make use of JQuery, although I couldn't see a way to do these things in that either.

I'm assigning this 500 points not because it's especially urgent (although I'd like to be able to put in production in early July) but because I think it's very difficult -- but maybe it's impossible or really simple, I dunno! :-)
Avatar of Graham Hirst
Graham Hirst
Flag of United Kingdom of Great Britain and Northern Ireland image

Give this a try, I know it works in IE 6/7 but not sure about the other browsers.
function onWinClose()
  {
     alert("Hello World!!!")
  }
 
<body onfocusin="CurrentObj=event.srcElement;" onbeforeunload="if((window.event.clientX<0) || (window.event.clientY<0)) onWinClose()">

Open in new window

First of all, getting the shorcut to start minimized is simple.  Just look at the properties of the shortcut,
In the field "Run", select "Minimized" and everytime that shortcut is run, it will start minimized.

The other one is trickier, but I'm sure I've done something like that before.  Gimme a few minutes to think :)
Forgot to mention that this is for the closing of the window.
Nice one FSIFM.  That will only work in IE but if that's all you need...  Just wanted to point out that that script will probably be blocked by default.  So you may have to adjust the security settings of the browsers.  The old porno pop-up trick :)
Avatar of Dave6969
Dave6969

ASKER

Wow... I never thought I'd get an answer that quickly!

The onclose function is lovely... very nearly what I want.

Although I need it to confirm rather than alert. I changed it from alert to confirm but it just popped it up again and and then again and then ended closing it anyway. I'm geuusing it would need some sort of return false/true logic. I'm afraid my javascript is only really good enough to modify other people's work! lol

Very nearly there, though -- and my colleagues said it was impossible!

The browsers *should* be set to make the intranet into a trusted zone, but I can't be 100% sure and also I don't have to authority to get it changed so I guess we'll have to do some testing.

As for minimizing, I'd forgotten about the shortcut settings, so that's a fallback position, although what I was thinking along the lines of was window that fired itself up, generated a popup which then closed the parent and then minimized itself... so that it always looks like a "proper" windows application (I shall work of the frameless windows later, but I want to get the nuts and bolts sorted first.
Oh... actually I've just noticed that an internet shortcut *won't* allow you to set the window state, so you can't minimize it on opening :-(
Oh ok.  You can create a shorcut to a program and in the target point it to your URL like this (change google to your url of course)

"C:\Program Files\Internet Explorer\iexplore.exe" http://google.ca
ASKER CERTIFIED SOLUTION
Avatar of Airyck666
Airyck666
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Sorry for the late response, had a bit of an emergency to deal with.

Thanks for that, and I see some of your points, unfortunately it doesn't work -- unless I set it up wrong. I'm now getting the page refreshing every xxxx milliseconds, but I already had a script to make the window pop up on the quarter hour (or any other division I set). And when I do close the window the dialogue has some standard text at the top and bottom that you can't change -- I could deal with it if the dialogue didn't ask you three times before it actually went away.

I actually got around the "navigate away from" / closing problem of the refreshing page by putting an iframe in the middle and having *that* refresh. It's not a perfect solution I know, as ideally this should be just one page.

FSIFM's solution is the closest to what I want it to be, but it still alerts rather than confirms, and when I do change it to confirm it still closes the window when I press cancel.

I also came across another issue while I've been playing around with it. Putting a shortcut in the startup folder isn't going to be enough as IE will crash every so often during the day (thanks Micro$oft). So I then made the app my home page... but then of course I lost my home page! So then I came up with the bright idea of kicking in a page which spawns the popup and then sends you to a selected page. Great. But now I'm having to minimize the popup manually again!

Do you ever get the feeling you're doing something you wish you hadn't started? ;-)
Haha, I get that feeling all the time.

I rewrote FSIM's solution for you using a confirmation.  Just know that this actualluy checks your mouse position and only presents the close confirmation if the mouse is off the client area.  The biggest benefit of this technique is it won't ask you to confirm when just clicking a link on the page.  

However:

- Selecting a hyperlink using the keyboard (if the mouse is off the page) will ask you to confirm.
- Pushing F5 with mouse in client area will reload without confirmation but move the mouse off the screen and press F5, you will be asked to confirm.
- Closing the window via the X (close) button will trigger the close confirmation, but the following will close the window WITHOUT confirmation:
     - File->Exit
     - Alt-F4
     - Right-click on the browser in the task bar -> Close

And the shortcut is the only way I can think of making it start minimized (other than writing an external program that fires the browser), since much of this is OS controlled and not browser.  Let me know if you have questions.  I can try to think of other ways.
<script type="text/javascript">
window.onbeforeunload = confirmClose;
 
function confirmClose() {
    if ((window.event.clientX<0) || (window.event.clientY<0)) {
        question = "Are you sure...?";
        return question;
    }
}
</script>

Open in new window

Clarification... above when I say that the script checks if the mouse is off the client area... it's actually checking if it is off to the left or above.  But not right and under.  That make sense?
Again apologies for the length of time in getting back to you.

I think that's going to be the solution. Although I have noticed another issue with my setup. If I make the browser default page my application, then minimize it it works fine... until I click on a link in an email in which case it tries to open it in the minimize widow.

I've opened a can of works here.

I'll accept your solution so yo9u get the points for all your hard work... however I may be posting more questions in the not-too-distant future!

Cheers!