Cửa sổ đang mở ()
ví dụ 1
Mở "www.w3schools.com" trong tab trình duyệt mới:
window.open("https://www.w3schools.com");
Thêm ví dụ bên dưới.
Định nghĩa và Cách sử dụng
Phương open()
thức này sẽ mở ra một cửa sổ trình duyệt mới hoặc một tab mới, tùy thuộc vào cài đặt trình duyệt của bạn và các giá trị tham số.
Xem thêm:
Cú pháp
window.open(URL, name, specs, replace)
Thông số
Parameter | Description | ||||||||||||||||||||||||
URL | Optional. The URL of the page to open. If no URL is specified, a new blank window/tab is opened |
||||||||||||||||||||||||
name | Optional. The target attribute or the name of the window. The following values are supported: | ||||||||||||||||||||||||
|
|||||||||||||||||||||||||
specs | Optional. A comma-separated list of items, no whitespaces. The following values are supported: | ||||||||||||||||||||||||
| |||||||||||||||||||||||||
replace |
DeprecatedSpecifies whether the URL creates a new entry or replaces the current entry in the history list. The following values are supported:
WarningChrome throws an exception when using this parameter. Source: . |
Giá trị trả lại
Tham chiếu đến cửa sổ mới hoặc null nếu cuộc gọi không thành công. |
Các ví dụ khác
Mở trang trống about: trong cửa sổ / tab mới:
var myWindow = window.open("", "", "width=200,height=100");
Mở một cửa sổ mới có tên "MsgWindow" và viết một số văn bản vào đó:
var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
Thay thế cửa sổ hiện tại bằng một cửa sổ mới:
var myWindow = window.open("", "_self");
myWindow.document.write("<p>I replaced the current window.</p>");
Mở một cửa sổ mới và kiểm soát giao diện của nó:
window.open("https://www.w3schools.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
Mở nhiều tab:
window.open("http://www.google.com/");
window.open("https://www.w3schools.com/");
Mở một cửa sổ mới. Sử dụng close () để đóng cửa sổ mới:
function openWin() {
myWindow = window.open("", "myWindow", "width=200,height=100"); // Opens a new window
}
function closeWin() {
myWindow.close(); // Closes the new window
}
Mở một cửa sổ mới. Sử dụng thuộc tính name để trả về tên của cửa sổ mới:
var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>This window's name is: " + myWindow.name + "</p>");
Sử dụng thuộc tính opener để trả về một tham chiếu đến cửa sổ đã tạo cửa sổ mới:
var myWindow = window.open("", "myWindow", "width=200,height=100"); // Opens a new window
myWindow.document.write("<p>This is 'myWindow'</p>"); // Text in the new window
myWindow.opener.document.write("<p>This is the source window!</p>"); // Text in the window that created the new window
Hỗ trợ trình duyệt
open()
được hỗ trợ trong tất cả các trình duyệt:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |