我有一个从服务器重新加载的功能:
location.reload(true)
目前,我在此行之后的任何代码都没有执行。
我很好奇是否有办法让更多代码运行之后 重新加载?
最佳答案
最好的方法是编写一个函数,如果设置了某些“GET”变量,则调用该函数并在每次运行时检查它们,例如 mypage.html?reload=true
甚至是哈希位置:mypage.html#reload
你可以像这样比较:
$(document).ready(function () {
if (window.location.hash == '#reload') {
onReload();
}
});
function onReload () {
console.log('test');
}
function reload () {
window.location.hash = 'reload';
window.location.reload();
}
只需调用
reload();
功能。输出(在控制台中):
test
然后您可以将人们从哈希
#reload
中重定向出去。如果您不想在页面再次刷新时运行您的函数:window.location.hash = '';
关于javascript - 我们可以在 location.reload(true) 之后有代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52192666/