JavaScript - How can i tell Browser Chrome, Firefox, Safari, to allow me to have the refresh button disabled?
Asked Answered
B

1

2

I have logical application running where i need to store the var algorithmToApply=1 or etc;

Where each of my value has relative algorithm assigned. The main problem is while testing people clicking on browser refresh button and all my application logic crash.

algorithmToApply = 1 ; // Thinking
algorithmToApply = 2 ; // Waiting
algorithmToApply = 11 ; // Downloading
algorithmToApply = 100 ; // Booting
algorithmToApply = 900 ; // Kernel prepare
algorithmToApply = 0 ; // User refresh button is a BUG

How can i using JavaScript request Browsers (Opera or Chrome optionally Firefox/Safari), to allow me to restrict user not able to click refresh (however they can always shutdown or close my browser instance).

Is this possible? If so how?

Follow up: (best we can do )

/* @WARNING: Do not refresh */
$(window).bind('beforeunload', function(e) {  
  if (mystatus>=2) {
   return "WARNING: Your status is .... If you refresh now, software will lose all the status.";
  } else {
   return "WARNING: Your status is not ... You can refresh.";
  }      
});
Bondwoman answered 15/3, 2012 at 20:6 Comment(2)
You're thinking the wrong way. Not "How can I disable the refresh button", but "How can I keep my information between page refreshes?" - Have you considered using localStorage/sessionStorage to keep track of state so that if the user refreshes the page, it picks up where it left off?Oreopithecus
No and you should not be relying on disabling the refresh button of the browser. If you're relying on this then you should really reconsider your application logic and how it's structured.Avrilavrit
G
3

That is not possible.

You can, however, add a beforeunload listener which prompts for confirmation:

var algorithmToApply = 0;
window.onbeforeunload = function() {
    if (algorithmToApply !== 0) { /* Anything ..*/
        return 'A task is pending. Do you really want to leave?';
    }
};
Gabelle answered 15/3, 2012 at 20:8 Comment(6)
@nnnnnn Unfortunately, not. However, most browsers support this, and it's the only way to prevent navigating away.Gabelle
I have just tested this feature, and updated the MDN's reference: developer.mozilla.org/en/DOM/… - All (ancient) major browsers are supported, except for Opera.Gabelle
Opera will add support for the beforeunload event, I expect it to land in some version after Opera 12 final.Santosantonica
@Santosantonica Can you post a reference for your statement? I can't find any.Gabelle
You can take my word for it :-) I work at Opera and have been keeping an eye on this particular issue since March 2004, according to the bug tracker.Santosantonica
@c69 it will be added because it has some (limited) use. Personally, I think using it means your architecture is wrong and you haven't prepared your app for unexpected problems (electricity loss? network interruptions? dog pushed the computer's power button? none of those events will fire your beforeunload handler, dear..), but maybe that's just me. Like any other event it can be tweaked or disabled with user javascript. I don't push for any separate preference for it since IMO user JS is easy enough for those who are power users enough to want to disable it.Santosantonica

© 2022 - 2024 — McMap. All rights reserved.