window.open(url, target, features) mở tab/cửa sổ mới. target: _blank (mặc định), _self, _parent, tên cửa sổ. features: chuỗi cấu hình (width, height, popup...). Trình duyệt chặn popup nếu không phải user gesture (click).
Ví dụ
Mở Google trong tab mới khi click:
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>window.open JS</title>
<style>
body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; }
</style>
</head>
<body>
<h1>Mở cửa sổ trình duyệt mới với window.open()</h1>
<br />
<button class="Btn">MỞ GOOGLE</button>
<h3>Nhấn nút để mở https://google.com trong tab mới</h3>
<script>
const btn = document.querySelector('.Btn');
btn.addEventListener('click', () => {
window.open('https://www.google.com', '_blank');
});
</script>
</body>
</html>
Kết quả
Tab mới mở google.com (nếu không bị chặn popup blocker).
Best practice: Chỉ gọi trong event handler (click). Tránh abuse. Dùng rel="noopener noreferrer" cho link <a target="_blank"> để an toàn.