目录
核心代码
location.reload();
代码说明
在JavaScript中使用以上代码,即可刷新页面。
其效果相当于点击了页面左上角的刷新按钮。
示例展示
当点击刷新按钮,发现所有添加的“你好”都消失了,即表示刷新成功
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!-- 给该按钮绑定一个点击事件
只要一点击该按钮,就立即执行“refresh()”函数 -->
<button onclick="refresh()">点击刷新</button>
<!-- 单词释义:refresh---刷新 -->
<script>
function refresh(){
// 刷新页面
location.reload();
}
// 设定一个定时器,每1000毫秒(即1秒),在屏幕上添加一个“你好”
// 当点击刷新按钮,发现所有添加的“你好”都消失了,即表示刷新成功
setInterval(function(){
var body = document.getElementsByTagName("body")[0]
body.innerHTML+=`你好!`
},1000)
</script>
</body>
</html>
应用场景
数据库中的数据发生改变时,前端通过刷新来更新显示的内容。
参考资料
核心:
菜鸟教程https://www.runoob.com/w3cnote/js-refresh-current-page.html
其他:
js刷新当前页面的5种方式 | CSDNhttps://blog.csdn.net/ding_dq/article/details/100560461
其他部分
其他可以实现通过JavaScript对页面进行刷新的方法,可参考以下链接的原文章。
js刷新当前页面的5种方式 | CSDNhttps://blog.csdn.net/ding_dq/article/details/100560461
js 刷新当前页面 | 菜鸟教程https://www.runoob.com/w3cnote/js-refresh-current-page.html