too long

When the refresh button is pressed, I would like to send the user to a different page using PHP or javascript. That is,

if (refresh pressed) header('Location: Some_Page.php');

Can anyone tell me how is it possible?

Although you tagged your question as javascript, you did also tag it as php

You could use sessions for this. Here is what I tried that worked.

I set the conditional statement to 10 (for testing purposes), but you can make it as 1 or any other number you wish.

N.B.: ob_start(); is required, otherwise it will throw an headers already sent error message.

<?php
ob_start();
session_start();

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
 echo "Views=". $_SESSION['views'];

if ($_SESSION['views']== 10){
header("Location: http://www.example.com/");
}
?>

Footnotes: If you use this, session_start(); (and maybe ob_start(); if using header()) needs to be inside all files using the same session, and at the top as shown.

So, I don't think you can hook into a specific page refresh event (I don't think it exists), you can hook into a page unload via javascript, which would be fired on a page refresh event. You would use the onunload or onbeforeunload event to trigger saving the state in cache or a cookie. (you could also on timeouts set the state every x seconds during normal gameplay)

You cannot alert or redirect with hooking into the unload or onbeforeunload events.


the following was my original suggestion, before I tested and found you cannot do the page navigation from the onunload and onbeforeunload events

You would also want to set a boolean saying game place has started, check for the boolean in the unload event (set that boolean to false after game is complete). That way you do not do a redirect when the user wants to actually navigate away from your game.

window.onbeforeunload=function(){window.location.replace(...)};

window.location.href=... would also work in the context of the onunload event.

Event click on Refresh button equals event of exiting current page. Use next:

 // jQuery
 $('body').bind('beforeunload',function(e){
   e.preventDefault();
   var url = "http://stackoverflow.com";    
   $(location).attr('href',url); 
 });

 // JS
 window.onbeforeunload = function (e) {
    e.preventDefault();
    var url = "http://stackoverflow.com";

    // IE8 and lower fix
    if (navigator.userAgent.match(/MSIE\s(?!9.0)/)) {
       var referLink = document.createElement("a");
       referLink.href = url;
       document.body.appendChild(referLink);
       referLink.click();
     }         
     else  window.location.replace(url); 
 };