Окно открыто ()
Пример 1
Откройте «www.w3schools.com» в новой вкладке браузера:
window.open("https://www.w3schools.com");
Ниже приведены дополнительные примеры.
Определение и использование
Метод open()
открывает новое окно браузера или новую вкладку, в зависимости от настроек вашего браузера и значений параметров.
Смотрите также:
Синтаксис
window.open(URL, name, specs, replace)
Параметры
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: . |
Возвращаемое значение
Ссылка на новое окно или null , если вызов не удался. |
Дополнительные примеры
Откройте страницу about:blank в новом окне/вкладке:
var myWindow = window.open("", "", "width=200,height=100");
Откройте новое окно с именем «MsgWindow» и напишите в нем текст:
var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
Замените текущее окно новым окном:
var myWindow = window.open("", "_self");
myWindow.document.write("<p>I replaced the current window.</p>");
Откройте новое окно и настройте его внешний вид:
window.open("https://www.w3schools.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
Откройте несколько вкладок:
window.open("http://www.google.com/");
window.open("https://www.w3schools.com/");
Откройте новое окно. Используйте close(), чтобы закрыть новое окно:
function openWin() {
myWindow = window.open("", "myWindow", "width=200,height=100"); // Opens a new window
}
function closeWin() {
myWindow.close(); // Closes the new window
}
Откройте новое окно. Используйте свойство name, чтобы вернуть имя нового окна:
var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>This window's name is: " + myWindow.name + "</p>");
Использование свойства opener для возврата ссылки на окно, которое создало новое окно:
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
Поддержка браузера
open()
поддерживается во всех браузерах:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |