Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3319
  • Last Modified:

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! :-)
0
Dave6969
Asked:
Dave6969
  • 6
  • 4
  • 2
1 Solution
 
FSIFMCommented:
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

0
 
Airyck666Commented:
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 :)
0
 
FSIFMCommented:
Forgot to mention that this is for the closing of the window.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Airyck666Commented:
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 :)
0
 
Dave6969Author Commented:
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.
0
 
Dave6969Author Commented:
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 :-(
0
 
Airyck666Commented:
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
0
 
Airyck666Commented:
Here you go, try this out.

The problem with FSISM's solution is that your page is going to be automatically refreshing periodically, and this auto-reload will trigger the onbeforeunload event.  And checking the clientX and clientY is realy only useful for checking if the user clicks the close button.  

So you'll get the confirmation dialog box wheneven the page auto-reloads and the mouse popinter is not within the client window.  Also you will NOT get the confirmation dialog if you right-click the minimized window on the task bar and click close.

But if you do your auto-reloading in javascript then you can set up a variable to exclude auto-reloads from triggering the confirmation box.  And removing the mouse pointer checks means that trying to close the window via right-click -> close WILL trigger the confirmation box.

As a side effect this will present the user with the confirmation box if they navigate from the page.  Hopefully that is okay with you.

Try it out.

Eric
<script type="text/javascript">
var autoRefresh = false;
// Set closure confirmation - Order seems important here.  Must set onbeforeunload first.  Don't know why.
window.onbeforeunload = confirmClose;
// Reload time.  In milliseconds.
window.onload = setTimeout("refresh()", 10000);
 
function refresh() {
    autoRefresh = true;
    window.location.reload(false);
}
 
function confirmClose() {
    if (!autoRefresh) {
        question = "Are you sure...?";
        return question;
    }
}
</script>

Open in new window

0
 
Dave6969Author Commented:
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? ;-)
0
 
Airyck666Commented:
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

0
 
Airyck666Commented:
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?
0
 
Dave6969Author Commented:
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!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now