1

I am currently working on one project in that if user try to refresh current page they will get confirm box with alert Don't refresh page or else you will lose data so if they cancel then he will stay on same page, but my problem is when they click on yes i.e Reload as he click reload one more alert should get you clicked reload now page will reload and data will loss, so how to do this because I am not getting any thing when he click on Reload button.

below I have written script.

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
    $(window).on('beforeunload', function(){ 
        return prompt("are you sure want to refresh this page !");

        if(true){
            alert('You will lose data');
        }
        else{
            alert("Ok continue your work");
       }
    });
</script>
3
  • "You will loose data"...should be "lose". "loose" means something else. Just a small thing to make your app look more professional. And the ! in "are you sure want to refresh this page !" should be a ?, and there shouldn't be a space before it. the "a" in "are" should also be capitalised ("Are") because it's the start of a sentence. Maybe if you are not a native English speaker you should ask someone to check your grammar and spelling. The wording of your question also lacks adequate punctuation and is difficult to follow. Commented Jul 30, 2018 at 9:39
  • 1
    You are doing return prompt() any code after a return statement will not be executed Commented Jul 30, 2018 at 9:40
  • @PatrickEvans ok please tell me how to solve issue even if i remove return still not getting anything. Commented Jul 30, 2018 at 9:42

2 Answers 2

1

A return statement exit a function so everything after a return statement is not executed

I don't think you want a prompt box but a confirm box (see here).

  • A prompt box is used only to get something by the user
  • A confirm box is used to ask the user if he confirms something

var rep = confirm("are you sure want to refresh this page !");
console.log(rep);

if(rep){
  alert('You will lose data');
}
else{
  alert("Ok continue your work");
}

Sign up to request clarification or add additional context in comments.

5 Comments

i want this should work when user try to refresh for that purpose i have mentioned onbeforeload in script
This will, just put the code i gave you in your $(window).on('beforeunload', function(){
just now i tried that whatever you mentioned still without any confirm box or alert directly refreshing page.
Browsers may block alerts, confirms and prompts inside the 'beforeunload' event in order to avoid annoying pop-ups.
That's what I was thinking about but without further information by the SO I cannot help more than with this answer
0

I would recommend you do not use Jquery when using onBeforeLoad event. It causing unwanted behavior. Vanilla JS would handle it better.

example:

window.onbeforeunload = function beforeExit();

function beforeExit(){
return confirm("Are you sure you want to exit?");
}

with this method, your function will execute if the user is trying to refresh the page.

3 Comments

it is not working can you please send me working code for this.
not getting expected output I mean not getting alert please check at your system if it works tell me what changes i have to do
@Anonymous, try with a console.log(). Alert might get blocked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.