Forum Moderators: open

Message Too Old, No Replies

         

g8z

5:34 pm on May 4, 2002 (gmt 0)



how does one detect the browser refresh event?

I'm making a program that requires that the user not refresh the screen - if
a refresh is done anyway, an error should appear.

DrDoc

6:22 pm on May 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if you can detect a refresh, at least not so it works cross-browser.

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.

joshie76

11:55 am on May 5, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi g8z, welcome to WmW.

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

david752

4:06 pm on May 14, 2002 (gmt 0)

10+ Year Member



I think this is not too hard to do.

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();

DrDoc

1:53 am on May 15, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ehm .. david,
the problem was to detect the REFRESH event, not the RESIZE event ;)

brotherhood of LAN

2:02 am on May 15, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



how about opening a new window for this scenario, and preventing the standard buttons/scrollbars from appearing. (cant remember the JS I used, hardly use the stuff)

That way they wont even have a choice about refreshing (but theyll need to have jscript turned on)

david752

3:23 pm on May 15, 2002 (gmt 0)

10+ Year Member



DrDoc, Oops. As Emily Littella used to say on SNL, "never mind."

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

sathya400

1:08 am on May 28, 2002 (gmt 0)



How to detect the browser refresh event? Pl. advice me..

Thanks...

DrDoc

1:55 am on May 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World, sathya400

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 ;)