window.location.href、window.location.replace 和 window.location.assign 在 JavaScript 中的区别是什么?
window 对象在 JavaScript 中包含 location 对象。它包含以下属性 −
window.location.href
它返回当前页面的 URL。
示例
<!DOCTYPE html>
<html>
<body>
<p>Click below to get the complete URL of the page.</p>
<button onclick = "display()">URL</button>
<script>
function display() {
var res = location.href;
document.write(res);
}
</script>
</body>
</html>window.location.replace
它用于替换当前文档。
示例
<!DOCTYPE html>
<html>
<body>
<button onclick = "display()">Replace current document</button>
<script>
function display() {
location.replace("https://www.qries.com")
}
</script>
</body>
</html>window.location.assign
如果你想加载一个新文档,请使用 JavaScript assign。
示例
<!DOCTYPE html>
<html>
<body>
<button onclick = "display()">Open new document</button>
<script>
function display() {
location.assign("https://www.qries.com")
}
</script>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP