JavaScript中的replace()方法用于將當前頁替換為另一頁,重定向頁面。將指定的URL作為參數(shù)傳遞給replace方法,即可使用該指定URL替換當前窗口的URL。

原文地址:JavaScript的replace()方法有什么用?
語法:
location.replace(URL)
參數(shù):此方法接受單個參數(shù)URL。URL是需要替換舊頁面的其他頁面的引用。
說明:replace() 方法不會在 History 對象中生成一個新的記錄。當使用該方法時,新的 URL 將覆蓋 History 對象中的當前記錄。
示例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h2>Replace方法</h2>
<button onclick="Replace()">重定向</button>
<script>
//Replace function that replace the current page.
function Replace() {
location.replace("https://www.html.cn/")
}
</script>
</body>
</html>
效果圖:

推薦閱讀: