Forum Moderators: open
I'm making a program that requires that the user not refresh the screen - if
a refresh is done anyway, an error should appear.
However, there are a couple of things you can do to prevent it from happening.
1) Let the page open in a new window.
2) Use the ONMOUSEDOWN and ONKEYDOWN events to check for mouse right-click, Shift-F10, the Context-Menu key, F5, and Ctrl-R .. (Are you familiar with how to check for those?)
3) Make proper use of the NOSCRIPT tag to redirect the user to a different page if they don't have JavaScript activated.
As you're probably beginning to find it's a bit of a nightmare to stop someone refreshing a page.
If the reason you need to do this is to prevent secondary posts (i.e. duplicate submits and so on) then there are other techniques you can use at the back end.
To prevent this in one of our applications we have a field on each table called the updatecount. This is downloaded to the page in a field and when the user posts the form the returned value is added to the where clause, the value is then incremented. If the updatecount does not match then there is an error. This example only works for updates on existing data rows but I'm sure you get the gist and could modify the idea for inserts too.
Josh
For most browsers, intercept the event:
onresize = Resize;
function Resize()
{
// show err msg
}
Opera 6.0 does not support onresize (amazing, no?). For Opera, monitor the window size (this code was not tested):
// Init
var origWidth = innerWidth; // for NN4
var origHeight = innerHeight;
function DetectResize()
{
if (innerWidth != origWidth ¦¦ innerHeight != origHeight) // for NN4
{
origWidth = innerWidth;
origHeight = innerHeight;
HandleResize();
}
}
function HandleResize()
{
alert('Sorry, you cannot resize this window.');
}
if (NN4)
onresize = DetectResize;
else if (Opera)
setInterval("DetectResize()", 1000);
else
HandleResize();
If I had to detect Refresh, I would use Server-side code that interfaces with a temporary file specific to that user (or possibly a cookie, if enabled, or a session variable) to record the page name or an ID when it is displayed for the first time. Then the onload handler can check to see if the page has already been loaded.
I believe you can only enforce this using server-side (PHP, ASP, etc.) code because the user can easily disable cookies and Javascript.
David
Thanks...
I am afraid that there's no way you can detect the browser refresh event. If there was, it still wouldn't do you any good since they user could just enter the URL in the location bar again to return to your page.
You can't stop that, or you'd have to stop people from visiting your site entirely ;)