1

I've searched high and low and am unable to find any scripts or instructions on how to have a page automatically refresh ONLY ONCE after initially being loaded/viewed.

I implemented a javascript code which states this: if the page is loaded completely, refresh the page immediately, but only once.

window.onload = function () {window.location.reload()}

But this gives a loop without the "only once".

I also Implemented meta refresh but it also gives a loop.

<meta http-equiv="refresh" content= "0;URL=http://www.example.com" />

Please tell me how can i use body onload refresh functionality only once that is for first time and not every time the php page is refreshed .

I appreicate your immediate response. Thanks

Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • In theory, I haven't tried it but you can make a loop that loops once... and then have the script run inside the loop? Just make sure you escape the loop afterwards and it won't re-loop inside – Katler Dec 10 '14 at 07:33
  • Why you need to reload the page??? – A. Wolff Dec 10 '14 at 07:35
  • @skibbi_bizzle, when the page is loaded completely. I want to refresh the page only once. – Debug Diva Dec 10 '14 at 07:36
  • @RohitJindal — You were asked "Why" not "Please repeat yourself". – Quentin Dec 10 '14 at 07:37
  • This is probably an X-Y problem where the solution to the actual problem is [this](http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers). – Quentin Dec 10 '14 at 07:38

3 Answers3

13

You should keep a flag in cookie, server session, or local storage. For example:

window.onload = function () {
    if (! localStorage.justOnce) {
        localStorage.setItem("justOnce", "true");
        window.location.reload();
    }
}
dashtinejad
  • 6,193
  • 4
  • 28
  • 44
  • Plus 1, was going to suggest the local storage option, but this is a perfect alternative. – Darren Dec 10 '14 at 07:38
  • nice solution, i have never used `localStorage` so far. the only downside of it could be that you can't decide if you want the page to be reloaded only the first time you visit it - or if you want to reload it again if the user visits another page on your site. that's why i kinda like my solution with the GET var more. – low_rents Dec 10 '14 at 07:44
  • 1
    How would you call this function? – lbriquet Dec 11 '14 at 14:32
0

i don't know why you wanna do that. but you can just use php, javascript/jquery together to approach this:

<?php
    if ($_GET["reloaded"] != 1) { 

        echo '
            <script>
                $(function () {
                    window.location.href = "my_page.php?reloaded=1";
                });
            </script>
       '; 

    }
?>

explanation: when you load the page for the first time $_GET["reloaded"] is not set, so the javascript is echoed. i used jquery's document ready function here, to be sure that your DOM is fully loaded before it reloads the page. when reloading you set the GET variable reloaded to 1.
thus php will not echo the javascript code again on reload.

low_rents
  • 4,481
  • 3
  • 27
  • 55
0

What you want to do is set a cookie, to check if the user has refreshed or not. The following is basically pseudo-code for how you should do it.

Javascript

if(!COOKIE_EXISTS){
    SET_COOKIE();
    REDIRECT_TO_DESIRED_PAGE;
}

Php

if(!isset($_COOKIE['page_was_refreshed'])){
    setcookie("CookieName", "CookieValue", time() + (10 * 365 * 24 * 60 * 60));
    die(header("Location: your/url/to/redirect"));
}
Darren
  • 13,050
  • 4
  • 41
  • 79