Định nghĩa và Cách sử dụngPhương thức open() 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ố. Mẹo: Sử dụng phương thức close() để đóng cửa sổ. Hỗ trợ trình duyệtTất cả các trình duyệt đều hỗ trợ open(). Cú phápwindow.open(URL, name, specs, replace) Giá trị tham sốURL Tùy chọn. Xác định URL của trang để mở. Nếu không có URL thì một cửa sổ mới/tab mới với about:blank được mở name Tùy chọn. Xác định tên của cửa sổ mới. Những giá trị sau được hỗ trợ:
specs Tùy chọn. Một danh sách các mục được phân cách bằng các dấu (,), không được có khoảng trắng. Các giá trị sau được hỗ trợ:
replace Tùy chọn. Dùng để xác định xem URL có tạo đầu vào mới thay thế đầu vào hiện tại trong danh sách lịch sử hay không. Những giá trị sau được hỗ trợ:
Chi tiết kỹ thuậtGiá trị trả về: Tham chiếu đến cửa sổ mới được tạo hoặc null nếu lời gọi không thành công. Các ví dụVí dụ sau mở một trang trắng ở một cửa số hoặc tab mới: <!DOCTYPE html> <html> <body> <p>Click the button to open an about:blank page in a new browser window that is 200px wide and 100px tall.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var myWindow = window.open("", "", "width=200,height=100"); } </script> </body> </html> Ví dụ sau mở một cửa số mới có tên MsgWindow và viết một số văn bản vào đó: <!DOCTYPE html> <html> <body> <p>Click the button to open a new window called "MsgWindow" with some text.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var myWindow = window.open("", "MsgWindow", "width=200,height=100"); myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>"); } </script> </body> </html> Ví dụ sau thay thế cửa sổ hiện tại bằng một cửa sổ mới: <!DOCTYPE html> <html> <body> <p>Click the button to put the new window in the current window.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var myWindow = window.open("", "_self"); myWindow.document.write("<p>I replaced the current window.</p>"); } </script> </body> </html> Ví dụ sau mở một cửa sổ mới và kiểm soát giao diện của nó: <!DOCTYPE html> <html> <body> <p>Click the button to open a new window with some specifications.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { window.open("https://v1study.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400"); } </script> </body> </html> Ví dụ sau mở nhiều cửa sổ cùng lúc: <!DOCTYPE html> <html> <body> <p>Click the button to open multiple tabs.</p> <button onclick="myFunction()">Open Windows</button> <script> function myFunction() { window.open("https://v1study.com/"); window.open("https://v1mart.com/"); } </script> </body> </html> Ví dụ sau mở một cửa sổ mới, sau đó dùng close() để đóng nó: <!DOCTYPE html> <html> <body> <button onclick="openWin()">Open "myWindow"</button> <button onclick="closeWin()">Close "myWindow"</button> <script> var myWindow; function openWin() { myWindow = window.open("", "myWindow", "width=200,height=100"); myWindow.document.write("<p>This is 'myWindow'</p>"); } function closeWin() { myWindow.close(); } </script> </body> </html> Ví dụ sau mở một cửa sổ mới, dùng thuộc tính name để trả về tên của nó: <!DOCTYPE html> <html> <body> <p>Click the button to create a window and then display the name of the new window.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var myWindow = window.open("", "MsgWindow", "width=200,height=100"); myWindow.document.write("<p>This window's name is: " + myWindow.name + "</p>"); } </script> </body> </html> Ví dụ sau sử dụng thuộc tính openner để trả về một tham chiếu đến cửa sổ đã tạo cửa sổ mới: <!DOCTYPE html> <html> <body> <p>Click the button to write some text in the new window and the source (parent) window.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var myWindow = window.open("", "myWindow", "width=200,height=100"); myWindow.document.write("<p>This is 'myWindow'</p>"); myWindow.opener.document.write("<p>This is the source window!</p>"); } </script> </body> </html> |