本文為大家介紹三種 js 刷新當前頁面的方法:

  • reload() 方法;
  • replace() 方法;
  • 頁面自動刷新;

方法1:reload() 方法

reload()方法用於刷新當前文檔。

reload() 方法類似於你瀏覽器上的刷新頁面按鈕。

location.reload();

 

方法2:replace() 方法

replace() 方法可用一個新文檔取代當前文檔。。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鳥教程(runoob.com)</title>
<script>
function replaceDoc()
{
    window.location.replace("http://www.runoob.com")
}
</script>
</head>
<body>

<input type="button" value="載入新文檔替換當前頁面" onclick="replaceDoc()">

</body>
</html> 


方法3:頁面自動刷新

頁面自動刷新:把如下代碼加入<head>區域中

<meta http-equiv="refresh" content="5">

其中5指每隔5秒刷新一次頁面。