Đối tượng cửa sổ bao gồm đối tượng vị trí trong JavaScript. Nó bao gồm các thuộc tính sau -
window.location.href
Nó trả về URL của trang hiện tại.
Ví dụ
<!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
Nó được sử dụng để thay thế tài liệu hiện tại.
Ví dụ
<!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
Nếu bạn muốn tải một tài liệu mới, hãy sử dụng JavaScript gán.
Ví dụ
<!DOCTYPE html>
<html>
<body>
<button onclick = "display()">Open new document</button>
<script>
function display() {
location.assign("https://www.qries.com")
}
</script>
</body>
</html>