JavaScript reload()基础教程文档
收录于 2023-04-20 00:10:05 · بالعربية · English · Español · हिंदीName · 日本語 · Русский язык · 中文繁體
在JavaScript中,
reload()方法用于重新加载网页。它类似于浏览器的刷新按钮。此方法不返回任何值。
语法
location.reload()
此方法可以具有可选参数
true 和
false 。
true 关键字强制从服务器重新加载页面,而
false 关键字从缓存重新加载页面。
false 是此方法的默认参数,因此,如果我们省略该参数的值,则
reload()方法将从缓存中重新加载页面。这意味着
object.reload()与
object.reload(false)相同。
我们来看一个使用
location.reload()方法。
示例
此处,函数
fun()包含
location.reload()方法。我们使用
button 元素的
onclick 属性调用函数
fun()。因此,我们必须单击给定的
HTML
"reload" 按钮以查看效果。单击按钮后,页面将重新加载。
<!DOCTYPE html>
<html>
<head>
<title>
location.reload() method
</title>
<script>
function fun() {
location.reload();
}
</script>
</head>
<body>
<p> Welcome to the lidihuo.com </p>
<p> This is an example of location.reload() method </p>
<p> Click the following 'Reload' button to see the effect. </p>
<button onclick = "fun()"> Reload </button>
</body>
</html>
<html>
<head>
<title>
location.reload() method
</title>
<script>
function fun() {
location.reload();
}
</script>
</head>
<body>
<p> Welcome to the lidihuo.com </p>
<p> This is an example of location.reload() method </p>
<p> Click the following 'Reload' button to see the effect. </p>
<button onclick = "fun()"> Reload </button>
</body>
</html>
输出
Welcome to the lidihuo.com
This is an example of location.reload() method
Click the following 'Reload' button to see the effect.
在在上面的示例中,我们也可以将
location.reload()方法附加到按钮标记上,而不是调用
fun()函数。可以按照以下步骤完成:
<button onclick = "location.reload()"> Reload </button>
使用上述语法,我们不需要创建任何
JavaScript函数来重新加载页面。