admin管理员组

文章数量:1220501

Is there any way to implement a callback for a page refresh function, check if the page is ready after it is refreshed and then print a message? I'd like to show refresh the page and show a certain message after clicking a button, however, I need to stay within my code in order to know what message to show. Anything similar to the next code would be great:

location.reload(function(){
   alert ('done refreshing and document is ready');
});

Is there any way to implement a callback for a page refresh function, check if the page is ready after it is refreshed and then print a message? I'd like to show refresh the page and show a certain message after clicking a button, however, I need to stay within my code in order to know what message to show. Anything similar to the next code would be great:

location.reload(function(){
   alert ('done refreshing and document is ready');
});
Share Improve this question edited Mar 16, 2017 at 11:00 T J 43.2k13 gold badges86 silver badges142 bronze badges asked Jun 4, 2013 at 20:10 Tommy NaidichTommy Naidich 7621 gold badge5 silver badges24 bronze badges 6
  • 4 On page reload, your JS application gets re-initialized and starts all over again, so you cannot have a callback. – Ayush Commented Jun 4, 2013 at 20:11
  • you should just reload/refresh some part of the page using ajax. – A. Wolff Commented Jun 4, 2013 at 20:13
  • Pass the message (or something indicating which message to show) to your script so it is there on the next load. – Brad Commented Jun 4, 2013 at 20:13
  • @roasted: That's what I've been trying to do, but then certailn plugins aren't loaded / not affecting the re-loaded fields. – Tommy Naidich Commented Jun 4, 2013 at 20:14
  • 1 @passionateCoder - That was a very passionate comment – Gabe Commented Jun 4, 2013 at 20:16
 |  Show 1 more comment

4 Answers 4

Reset to default 13

On page reload, your JS application gets re-initialized and starts all over again, so you cannot have a callback.

However, what you can do is add a hash fragment to the URL before reloading.

window.location = window.location.href + "#refresh";
window.location.reload();

Then, on page load, check if the hash fragment exists. If it does, you'll know you just refreshed the page.

If you want to user window.location.reload() you have to use Browser's sessionStorage. Here is my code, I use this for saving data.

window.onload = function () {

    var msg = sessionStorage.getItem("nameShah");

    if (msg == "Success") {

        sessionStorage.setItem("nameShah", "");

        alert("Saved Successfully");
    }
}

$("#save").click(function () {

    $.ajax({
        url: "/AspNetUsers/SaveData",
        type: "POST",
        async: true,
        data:{userName: "a"},
        success: function (data) {

            if (data.Msg == "Success") {

                sessionStorage.setItem("nameShah", "Success");
                window.location.reload();
            }         
        }
    });
});

Server Side:

    public ActionResult SaveData(string userName)
    {
        return Json(new { Msg = "Success" }, JsonRequestBehavior.AllowGet);
    }

The way to go is using

$(document).ready(function(){
    alert('done refreshing and document is ready');
});

But it doesn't differentiate a first load from a refresh. But you could achive that using sessions. For example, using PHP:

<?php

session_start();

$showJs = false;

if( !isset($_SESSION['loaded_before']) ) {
    $showJs = true;
    $_SESSION['loaded_before'] = true;
}

?>
...
<?php if($showjs) { ?>
<script type="text/javascript">

    $(document).ready(function(){
        alert('done refreshing and document is ready');
    });

</script>
<?php } ?>

If you really want to go javascript side, you can use sessionStorage.varName before reloading, and then you check that var and continue.

本文标签: javascriptA callback for a page refresh functionStack Overflow