Cửa sổ mở()
ví dụ 1
Mở "www.example.com.vn" trong tab trình duyệt mới:
window.open("https://www.example.com.vn");
Hãy tự mình thử »Thêm ví dụ dưới đây.
Sự miêu tả
Phương thức open()
sẽ mở 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: Bugs Chromium Issue 1164959 . |
Giá trị trả về
Tham chiếu đến cửa sổ mới hoặc null nếu cuộc gọi không thành công. |
Thêm ví dụ
Mở trang about:blank trong cửa sổ/tab mới:
var myWindow = window.open("", "", "width=200,height=100");
Hãy tự mình thử »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>");
Hãy tự mình thử »Thay thế cửa sổ hiện tại bằng cửa sổ mới:
var myWindow = window.open("", "_self");
myWindow.document.write("<p>I replaced the current window.</p>");
Hãy tự mình thử »Mở một cửa sổ mới và kiểm soát sự xuất hiện của nó:
window.open("https://www.example.com.vn", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
Hãy tự mình thử »Mở nhiều tab:
window.open(" https://www.google.com/ ");
window.open("https://www.example.com.vn/");
Hãy tự mình thử »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
}
Hãy tự mình thử »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>");
Hãy tự mình thử »Sử dụng thuộc tính opener để trả về 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ãy tự mình thử »Hỗ trợ trình duyệt
open()
được hỗ trợ trong tất cả các trình duyệt:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |