Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing F5 from reloading the page.

Status
Not open for further replies.

LFI

Programmer
Apr 27, 1999
1,996
US
Hey, all.

I seem to be able to catch F5 when it's pressed (calling a function using the BODY tag's onkeydown event and checking for a keyCode of 116), but I still can't seem to prevent the page from reloading.

This is all I've tried so far:

Code:
<html>
<head>
<script>
function rejectF5()
{
 if(event.keyCode == 116)
  return false;
}
</script>
</head>
<body onkeydown='return rejectF5();'>
<script>
document.write(new Date()); [b]//I use this to see if page reloads.[/b]
</script>
</body>
</html>

Is it (what I want to do) do-able?

Thanks.

--Dave
 
Hey Dave,
(just so that you know), what makes Jeff's code stop the postback is the "return false"; line at the end of the function. Thus, whenever you have a situation where a postback occurs and you want to stop it (such as clicking a button), simply say return false right after it, and that will do the trick. Take a look at this:
Code:
btn.Attributes.Add("onclick", 
   "someFunc(); [b]return false;[/b]);
JC


Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
JC,

In this case, it was a keyboard key that was posting back. I don't think I can add an attribute to that.

I did have a 'return false' on my first attempt. The line that made it work for me is changing the keyCode to 0 so that when postback happened, the direction was to do nothing. If I comment out the 'return false,' it stops working again, so both lines are needed here.

This is what I ended up with (for a test):

Code:
<html>
<head>
<script>
function rejectF5()
{
 if(event.keyCode == 116)
 {
  event.keyCode = 0;
  return false;
 }
}
</script>
</head>
<body onkeydown='return rejectF5();'>
<script>
document.write(new Date());
</script>
</body>
</html>

Thanks for your post.

--Dave
 
Dave,
I didn't mean you could add attributes to the keyboard event. I meant that return false stops a postback. I didn't know you needed to change the keyCode to 0 in this case, though, so thanks for clearing that up!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 

Would using:

Code:
event.cancelBubble = true;

work instead of assinging 0 to event.keyCode?

Just a theory ;o)

Dan
 
Dan,

I just tried it. It did not work. Thanks for the suggestion though!

The solution to change the keyCode is IE-specific (according to Jeff's original post at the other end of the link, above, anyway) and that's not a problem for me where I work, but I would be interested in a multi-browser-compatible solution.

--Dave
 

There are so many ways of refreshing a page...

You'd have to stop right-click (so the user couldn't refresh using the menu), and open in a menuless window, so the user couldn't click "View" then "Refresh".

Too many things to trap ;o(

Dan
 
Jeff, Dan...

You guys are right. I was actually asking the question for a co-worker. He began seeking an alternate route to his problem anyway, but once I "figured out" how to "disable" F5, he seemed to consider going back to that.

I'll remind him of all these other methods of refreshing the page.

Thanks!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top