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 小寒健康祝福语大全简短