Google Answers Logo
View Question
 
Q: Run my function when Refresh button / F5 is pressed in the browser ( No Answer,   2 Comments )
Question  
Subject: Run my function when Refresh button / F5 is pressed in the browser
Category: Computers > Programming
Asked by: sansahu-ga
List Price: $10.00
Posted: 25 May 2006 11:12 PDT
Expires: 28 May 2006 00:06 PDT
Question ID: 732346
I want to run my own JavaScript function when the page tries to
refresh (either by F5 or clicking refresh button on the browser) and
block the refresh action (must be cross-browser compatible). It would
be better with an example.

Thanks!
Sanket
Answer  
There is no answer at this time.

Comments  
Subject: Re: Run my function when Refresh button / F5 is pressed in the browser
From: gdprasad-ga on 25 May 2006 20:55 PDT
 
Hi sansahu,
Please try with this example


<html>

<body  onKeyDown="checkSpecialCharacter()">

<center>

<script>

//verify for netscape/mozilla

var isNS4 = (navigator.appName=="Netscape")?1:0;

function checkSpecialCharacter()
{

if(window.event && window.event.keyCode == 116) 
   { 
   // Capture and remap F5
	window.event.keyCode = 505;
  	}

if(window.event && window.event.keyCode == 505) 
   { // New action for F5
	
		checkF5();
	return false; 
        // Must return false or the browser will refresh anyway
	}



}

function checkF5()
{
alert('F5   key was pressed by  sansahu');
}
</script>


</body>

</html>

Regards
gdprasad
Subject: Re: Run my function when Refresh button / F5 is pressed in the browser
From: sansahu-ga on 26 May 2006 11:18 PDT
 
Hi there,

Its working for the F5 button in IE. But, not in firefox :(. I am
searching the javascript for controlling browsers Refresh Button. As
you can seen it on http://pages.google.com. When you create a webpage,
while working on it if you refresh the page the refresh button is
trapped and the save function is executed.

Thanks!
Sanket

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy