Can we have code after location.reload(true)?

∥☆過路亽.° 提交于 2021-02-19 03:29:06

问题


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?


回答1:


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 compare 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 = '';




回答2:


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 complete 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!




回答3:


________________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>


来源:https://stackoverflow.com/questions/52192666/can-we-have-code-after-location-reloadtrue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!