1

I have the following code:

<html>
<head>
</head>

<body>
  <form method="post" action="something.php" name="myForm">
    <input type="submit" name="homepage" value="Please Work" id="homepage">
  </form>
<script>
  var btn = document.getElementById('homepage'),
  clicked = false;

  btn.addEventListener('click', function () {
      clicked = true;
  });

  window.onbeforeunload = function () {
      if(!clicked) {
          return 'If you resubmit this page, progress will be lost.';
      }
  };
</script>
</body>
</html>

This code pops a confirmation box when the user leaves the page (Back or Refresh Browser Button) but not when a button inside the form is clicked.

QUESTION How can I redirect to a different page when the user clicks the "Leave this Page" button of the confirmation box? I tried different methods but it does not work. I'm using Google Chrome.

Any help is very much appreciated. Thank you in advance. :)

EDIT

So I got some codes that actually works but it does the opposite. When I click the "Leave this Page" it stays and when I click "Stay on this Page" it redirects.

var onUnloadClick = 0;

function redirectTimer() {
    setInterval(function(){window.location = "sample exam.php"},10);
}

var btn = document.getElementById('homepage'),
clicked = false;

btn.addEventListener('click', function ()   {
    clicked = true;
});

window.onbeforeunload = onbeforeunload_Handler;

function onbeforeunload_Handler() {
    if(!clicked) {
        if(onUnloadClick == 0) {
            redirectTimer();
            return 'If you resubmit this page, progress will be lost.';
        }
    }
};

QUESTION How will I modify this code and do the opposite function?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Nek
  • 241
  • 4
  • 16
  • I get "Stay on this Page" or "Leave this Page" as the options in the confirmation box (in chrome). Nothing that says "OK/CONFIRM" Are you sure about your question? – Enigmadan Jan 13 '15 at 01:05
  • OK/CONFIRM is the same as "Leave this Page". Sorry for not putting the exact words but yeah I'm sure about my question. :) – Nek Jan 13 '15 at 01:09
  • It is not possible to change the functionality of the "Leave this Page" button. The user is in command of where they have chosen to go. Your prompt simply asks whether they are sure. What is it you are actually trying to do? – Enigmadan Jan 13 '15 at 01:31
  • I'm doing an examination program in php. When the user starts his/her exam, he/she will not be able to leave the exam unfinished. If he/she tries to click the back or refresh button, a confirmation box (the code I gave above) will pop telling that the user will have a grade of 0 if he/she wishes to leave the exam unfinished. If the user still leaves the page, that is where the redirect comes in. The user will be redirected to the homepage. Any idea about how to use onUnload? I think that would work but I have limited knowledge so I'm trying to find resources. – Nek Jan 13 '15 at 01:49

2 Answers2

1

You can't mess with the browsers processes. If you click back, it will go back. If you click refresh, it will refresh the page. The reason they're in there is because they have their functions. You can try different approach in doing what you want but I don't think this one is possible.

0

I dont think that its possible. Check out answer here: Intercept selection in window.onbeforeunload dialog

There is not defined way to intercept the onbeforeunload event. Further reference can be seen here: Intercept selection in window.onbeforeunload dialog

When onbeforeunload event is kick started the user has already selected where he\she wants to go; it would not make sense if the browser allows javascript on a webpage to override the actions of the user.

Hence there should not be anyway for you take the user to some other location other than where the user wants to go.

Updated Answer to the Updated Questions: Its not possible to do what you are trying to do. The reason you get redirected when you choose "stay on this page" and not otherwise is because your javascript function to redirect comes into effect only why you stay on the page. When you choose to leave the page the browser will no longer execute any of your code. Because the browser does not want you to override the choice fo the user.

My recommendation: Rethink what you want. You want to redirect the user to a different page when the user is trying to go somewhere else. I really don't think that is the correct.

What is your ultimate goal? If you let me know that.. probably I can provide you with a better solution.

Community
  • 1
  • 1
owaism
  • 1,118
  • 1
  • 8
  • 21
  • Oh I see.. Is there anything that I can do to achieve what I want to do? an onUnload perhaps? But I don't why it doesn't work. I tried onUnload but it does not work. Not sure if onUnload doesn't work or the redirect code itself. I used location.replace, location.host, etc but with no luck. – Nek Jan 13 '15 at 01:21
  • Read your comment on what you are actually trying to do. – owaism Jan 13 '15 at 16:06
  • @Nek A requirement is a requirement. But in my opinion your requirement is not right. If the student puts in google.com on the browser Address bar there should be absolutely no way for anyone to redirect the student to some other page. Same thing applies to when you try to close the browser window, there should be no way that the web page takes the user to some other page. Think about it... you are browsing some webpage lets say amazon.com and you try to go to google.com or try and close the webpage and amazon.com redirects you to zappos.com. That would be so annoying. Hence the restriction. – owaism Jan 13 '15 at 18:51
  • That is the exact reason why I am doing this requirement. My goal is to prevent the student on leaving the exam unfinished because the student will have a grade of 0. Now if he/she still leaves (even if he knew because of the popup), then he/she will get a grade of 0. My goal here is to prevent cheating because the questions provided are fetched from the database randomly so I should prevent the user from going back or refresh the page to changed the questions, hence my problem. I hope you can help me. – Nek Jan 14 '15 at 00:58
  • @Nek Now I understand your final goal. Client side restrictions like your trying to put do not solve your problem. The student can always stay on the same page and open up a new tab in the same browser to get a new set of questions and can very well answered those questions and forget about the previous page and shut the machine down. What I am trying to say essential is that the goal that you are trying to reach can be achieved only by putting in Server Side Logic. I can give one of the solutions that you can implement to get this done on the server side if you are interested. Let me know. – owaism Jan 14 '15 at 02:07
  • I don't have enough knowledge to fully understand what you are trying to say but i'll give it a shot. Please lead me to a solution for my problem here. Thank you so much. – Nek Jan 14 '15 at 03:04
  • @nek you need to store the questions generated for a student in the database and if the student refreshs\logs in again the same set of questions should be provided. Also this is beyond the scope of the question u have asked. Hope the answer I provided led you in the right direction. – owaism Jan 15 '15 at 02:18