JavaScript中window.location.href,window.location.replace和window.location.assign之间的区别?
窗口对象包括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
如果要加载新文档,请使用JavaScriptAssign。
示例
<!DOCTYPE html>
<html>
<body>
<button onclick = "display()">Open new document</button>
<script>
function display() {
location.assign("https://www.qries.com")
}
</script>
</body>
</html>热门推荐
10 朋友搬迁新房祝福语简短
11 给女儿祝福语简短精辟
12 简短祝福语蛋糕简笔画
13 生日祝福语简短蛋糕上
14 开业经典祝福语简短
15 哥哥新婚祝福语创意简短
16 关于扬州的祝福语简短
17 收围巾的祝福语简短
18 新年祝同学祝福语简短