JavaScript后刷新页面并运行功能 [英] Refresh page and run function after JavaScript

查看:53
本文介绍了JavaScript后刷新页面并运行功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试刷新我的页面,然后在刷新完成后运行一个函数.但是,我现在拥有的代码将运行funxtion,然后仅刷新,这意味着我失去了该函数的功能.有办法解决吗?

Hello I am trying to refresh my page and then run a function once the refresh has been completed. However the code I have now runs the funxtion then it only refreshes meaning I lose what the function does. Is there a way to solve it?

我的代码是:

function reloadP(){
    document.location.reload();
    myFunction();
}

<button onclick: "reloadP()">Click</button>    

推荐答案

页面加载后,您需要调用 myFunction().

You need to call myFunction() when the page is loaded.

window.onload = myFunction;

如果只想在页面重新加载时运行它,而不是第一次加载时运行,则可以使用 sessionStorage 传递此信息.

If you only want to run it when the page is reloaded, not when it's loaded for the first time, you could use sessionStorage to pass this information.

window.onload = function() {
    var reloading = sessionStorage.getItem("reloading");
    if (reloading) {
        sessionStorage.removeItem("reloading");
        myFunction();
    }
}

function reloadP() {
    sessionStorage.setItem("reloading", "true");
    document.location.reload();
}

演示

这篇关于JavaScript后刷新页面并运行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆