最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can we have code after location.reload(true)? - Stack Overflow

programmeradmin2浏览0评论

I have a function that does a reload from server with:

location.reload(true)

Currently, any code I have after this line is not executed.

I'm curious if there is a way to be able to have more code run after the reload?

I have a function that does a reload from server with:

location.reload(true)

Currently, any code I have after this line is not executed.

I'm curious if there is a way to be able to have more code run after the reload?

Share Improve this question edited Sep 6, 2018 at 0:41 GMachado 80111 silver badges24 bronze badges asked Sep 5, 2018 at 20:04 deal tekdeal tek 1295 silver badges15 bronze badges 5
  • 2 When you reload the page, the script also reloads, like a program restarting. Thus, the code doesn't run after it. You probably shouldn't reload if you want to do something after that. Can you give us some context? – clabe45 Commented Sep 5, 2018 at 20:07
  • Answer exists here stackoverflow./questions/41904975/… – CaseyC Commented Sep 5, 2018 at 20:10
  • I don't understand. What does your extra code do? Can't you execute the code first and THEN reload? – Damián Pablo González Commented Sep 6, 2018 at 0:50
  • Are you sure you even need the location.reload(true)? It's 2018 now, AJAX is well supported, no need to re-fetch the whole page when only some of the content has updated. – Kaiido Commented Sep 6, 2018 at 1:25
  • Related: Does JavaScript reload() stop the remainder of the page from being processed?, Why is the content not showing after refresh. – Sebastian Simon Commented Oct 21, 2022 at 22:39
Add a ment  | 

3 Answers 3

Reset to default 3

The best way to do this would be to write a function that is called if there are certain "GET" variables set and to check them on each run such as mypage.html?reload=true

Or even hash locations: mypage.html#reload

And you can pare like so:

$(document).ready(function () {
  if (window.location.hash == '#reload') {
    onReload();
  }
});

function onReload () {
  console.log('test');
}

function reload () {
  window.location.hash = 'reload';
  window.location.reload();
}

Just call the reload(); function.

Output (in console): test

And then you can just redirect people away from the hash #reload if you don't want your function to run if the page is refreshed again: window.location.hash = '';

why you need execute something after reload the page?

If you need to do something after reload, so you can pass some parameter to the page and check this to execute something additional. You can redirect to the same page and pass a plete url

var url = window.location.href; 
url += '&param=1'
window.location.href = url;

So, when your page reload, you can check a parameter by GET and do whatever you want. I hope this trick help you!

________________METHOD 1: WITH PHP_________________

1) reloading with a parameter

2) getting that parameter with PHP

For example:

window.location.href += '&reloading=1';
//it's equivalent to:
//window.location.href = window.location.href + '&reloading=1';

And then:

<?php
if (isset($_GET['reloading'])) {
    echo '<script>pageReloaded();</script>';
}
?>

Since PHP will be executed first, that little script (the function call) will be writen and then executed in javascript, so pageReloaded() function will be called.

A full example would be:

<?php if (isset($_GET['reloading'])) echo '<script>pageReloaded();</script>'; ?>
<script>
    function reloadNow(){
        alert("I'm about to reload");
        window.location.href += '&reloading=1';
    }
    function pageReloaded(){
        alert("I've been reloaded!");
    }
</script>

<button onclick="reloadNow()">Press to reload</button>

________________METHOD 2: NO PHP, ONLY JAVASCRIPT_________________

Using SessionStorage

1) Set SessionStorage.

2) Reload

3) Get that SessionStorage

For example:

<script>
    function reloadNow(){
        alert("I'm about to reload");
        sessionStorage.setItem('reloading',"true");
        window.location.reload(true);
    }

    //when loading:        
    if (sessionStorage.getItem('reloading') === "true") {
        sessionStorage.clear(); //so it doesn't trigger next time
        alert("I've been reloaded!");
    }
</script>

<button onclick="reloadNow()">Press to reload</button>
发布评论

评论列表(0)

  1. 暂无评论