Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent User to reload page using jquery or javascript [duplicate]

Possible Duplicate:
Prevent any form of page refresh using jQuery/Javascript

how can i prevent user to reload (refresh) html page using javascript or jquery.

after loading the page for the first time, i want to prevent user reload the page either by press F5 or refresh from browser or by any way.

Can i do that?

Note: i try window.onbeforeunload, but i didn't know how to stop window.onload event from it.

Any help?

like image 891
Georgian Citizen Avatar asked Jun 19 '12 05:06

Georgian Citizen


People also ask

How do I stop people from reloading my page?

What preventDefault() does is that it tells the browser to prevent its default behaviour and let us handle the form submitting event on the client side itself.

How do I stop a page from refreshing JavaScript?

const onConfirmRefresh = function (event) { event. preventDefault(); return event.

How do I stop jQuery from refreshing?

You can use event. preventDefault() to prevent the default event (click) from occurring. Is the same thing will apply for textbox on enter keypress? It should, whatever event is fired it prevents the default action.

How do you reload a page only once is JavaScript?

Finally, if you call the “reloadPage();” function in your Javascript file, it will only reload the page once.


1 Answers

It is not possible to stop the user from page reload/page close if the user wants it. Two methods which you can use are as follows:

You can ask for a confirmation like this:

<script>
window.onbeforeunload = function (e) {
e = e || window.event;

// For IE and Firefox prior to version 4
if (e) {
    e.returnValue = 'Sure?';
}

// For Safari
return 'Sure?';
};
</script>

Here is the fiddle for above: http://jsfiddle.net/gopi1410/NujmL/

You can preventDefault on pressing of F5 key using:

document.onkeydown = function(event) {
  if(window.event){
        event.preventDefault();
  }
}
like image 90
gopi1410 Avatar answered Nov 03 '22 06:11

gopi1410