From: Andrew Poulos on
I'm building a app in which opens in a new window without a menu bar or
tool bar. I need to warn users (who press the F5 key or Ctrl+R) that
refreshing may result in loss of data and to give them the option of
cancelling the refresh.

The code is relatively straightforward and works for IE, FF and Opera
but the intercept code is ignored by Safari 3.1 for Windows.

Following is part of the code that I'm using.

<script type="text/javascript">var sType = "keypress";</script>

<!--[if IE]>
<script type="text/javascript">sType = "keydown";</script>
<![endif]-->

<script type="text/javascript">

fIntercept = function(e) {
e = e || event.e;
if (e.keyCode == 116) {
// When F5 is pressed
fCancel(e);
} else if (e.ctrlKey && e.keyCode == 82) {
// When ctrl is pressed with R
fCancel(e);
}
};

fCancel = function(e) {
if (e.preventDefault) {
e.stopPropagation();
e.preventDefault();
} else {
e.keyCode = 0;
e.returnValue = false;
e.cancelBubble = true;
}
return false;
};

fAddEvent = function(obj, type, fn) {
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else {
obj['e'+type+fn] = fn;
obj[type+fn] = function() {
obj['e'+type+fn](window.event);
}
obj.attachEvent('on'+type, obj[type+fn]);
}
};

fAddEvent(document, sType,fIntercept);

</script>


Is there anything that can be done to get Safari to behave?

Andrew Poulos
From: pr on
Andrew Poulos wrote:
> I'm building a app in which opens in a new window without a menu bar or
> tool bar. I need to warn users (who press the F5 key or Ctrl+R) that
> refreshing may result in loss of data and to give them the option of
> cancelling the refresh.
>
> The code is relatively straightforward and works for IE, FF and Opera
> but the intercept code is ignored by Safari 3.1 for Windows.
>
> Following is part of the code that I'm using.
>
> <script type="text/javascript">var sType = "keypress";</script>
>
> <!--[if IE]>
> <script type="text/javascript">sType = "keydown";</script>
> <![endif]-->

[...]

>
> Is there anything that can be done to get Safari to behave?

Not wanting to stir up that whole JavaScript Ninja thing again :-)
nevertheless you might find this useful: <URL:
http://ejohn.org/blog/keypress-in-safari-31/>.
From: VK on
On Apr 22, 10:44 am, Andrew Poulos <ap_p...(a)hotmail.com> wrote:
> I'm building a app in which opens in a new window without a menu bar or
> tool bar. I need to warn users (who press the F5 key or Ctrl+R) that
> refreshing may result in loss of data and to give them the option of
> cancelling the refresh.

What about right-click - context menu - Refresh or direct address bar
typing? (the address bar cannot be removed for IE7/8 for security
consideration).

I would highly suggest to use onbeforeunload instead that covers all
navigation away situations.

For Safari and Opera where "onbeforeunload" property is spoofed but
not implemented: it is just too bad for the leftovers of their
remaining users. Eventually they will migrate on some more usable
platforms - it takes just 2-3 big almost filled forms lost on
different sites as the practice shows. Some may have a different
opinion.

// IE + Firefox:

window.onbeforeunload = warnNavigateAway;

function warnNavigateAway() {
var message = ''.concat(
'You are attempting to navigate away ',
'from the current page.\n',
'If you leave now then ',
'all current data will be lost.');

if ((typeof event == 'object') && ('returnValue' in event)) {
event.returnValue = message;
}
else {
return message;
}
}
From: VK on
On Apr 22, 4:49 pm, VK <schools_r...(a)yahoo.com> wrote:
> // IE + Firefox:

// + Camino of course for MacOS users

> window.onbeforeunload = warnNavigateAway;
>
> function warnNavigateAway() {
> var message = ''.concat(
> 'You are attempting to navigate away ',
> 'from the current page.\n',
> 'If you leave now then ',
> 'all current data will be lost.');
>
> if ((typeof event == 'object') && ('returnValue' in event)) {
> event.returnValue = message;
> }
> else {
> return message;
> }
>
> }
From: Thomas 'PointedEars' Lahn on
VK wrote:
> On Apr 22, 4:49 pm, VK <schools_r...(a)yahoo.com> wrote:
>> // IE + Firefox:
>
> // + Camino of course for MacOS users

There is also a Firefox version for Mac OS X. It would be easier (and
correct) to say Mozilla/5.0 (the codebase) or Gecko (the layout engine),
because there are much more browsers based on either than just Firefox and
Camino.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>