HTML中的DOM位置replace()方法用于将当前文档替换为指定的文档。此方法与assign()方法不同,因为它会从文档历史记录中删除当前文档,因此无法使用“后退”按钮返回到上一个文档。
用法:
location.replace( newUrl );
参数:
newUrl:这是用来替换当前页面的URL。这是一个必需的参数。
注意:要替换的URL必须允许成帧。
返回值:没有返回值。
例:
DOM Location replace() MethodGeeksforGeeks
DOM Location replace() Method
Click the button to replace
the current document with a new one.
Change Page
function changePage() {
// change the document to
// the GeeksforGeeks homepage
location.replace(
"https://ide.geeksforgeeks.org");
}
注意:在ide.geeksforgeeks.org上运行上述代码,因为代码中使用的链接是ide.geeksforgeeks.org以便进行取景。
输出:
按下按钮之前:
按下按钮后:
支持的浏览器:下面列出了DOM Location replace()方法支持的浏览器:
谷歌浏览器
IE浏览器
火狐浏览器
Opera
苹果浏览器