I have written the following javascript code to catch F5 key press and prevent the user from refreshing:
(I understand this is not a good idea but i am stuck with this whether any one likes it or not. Also the question i ask pertains to why the script below does not work with safari 4 but works for other browsers.)
var fn = function (e)
{
if (!e)
var e = window.event;
var keycode = e.keyCode;
if (e.which)
keycode = e.which;
var src = e.srcElement;
if (e.target)
src = e.target;
// 116 = F5
if (116 == keycode)
{
// Firefox and other non IE browsers
if (e.preventDefault)
{
e.preventDefault();
e.stopPropagation();
}
// Internet Explorer
else if (e.keyCode)
{
e.keyCode = 0;
e.returnValue = false;
e.cancelBubble = true;
}
return false;
}
}
// Assign function to onkeydown event
document.onkeydown = fn;
The above code works perfectly for IE 6,7 and 8, from Firefox 2.0 onwards and also in Chrome. However it does not work for Safari 4 for windows. Pressing the F5 key refreshes the document. The funny thing is that in Safari the code gets inside the if (e.preventDefault) part above but for some reason its not preventing the default action i.e. refreshing the page.
Is this a bug with Safari 4 or is there some safari specific code i need to write?
Thanks