Otevřené okno ()
Příklad 1
Otevřete „www.w3schools.com“ na nové kartě prohlížeče:
window.open("https://www.w3schools.com");
Další příklady níže.
Definice a použití
Metoda open()
otevře nové okno prohlížeče nebo novou kartu v závislosti na nastavení vašeho prohlížeče a hodnotách parametrů.
Viz také:
Syntax
window.open(URL, name, specs, replace)
Parametry
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: . |
Návratová hodnota
Odkaz na nové okno nebo null , pokud se volání nezdařilo. |
Další příklady
Otevřít about:prázdnou stránku v novém okně/záložce:
var myWindow = window.open("", "", "width=200,height=100");
Otevřete nové okno s názvem "MsgWindow" a napište do něj nějaký text:
var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
Nahradit aktuální okno novým oknem:
var myWindow = window.open("", "_self");
myWindow.document.write("<p>I replaced the current window.</p>");
Otevřete nové okno a ovládejte jeho vzhled:
window.open("https://www.w3schools.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");
Otevřít více karet:
window.open("http://www.google.com/");
window.open("https://www.w3schools.com/");
Otevřete nové okno. Pomocí close() zavřete nové okno:
function openWin() {
myWindow = window.open("", "myWindow", "width=200,height=100"); // Opens a new window
}
function closeWin() {
myWindow.close(); // Closes the new window
}
Otevřete nové okno. Pomocí vlastnosti name vrátíte název nového okna:
var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>This window's name is: " + myWindow.name + "</p>");
Použití vlastnosti opener k vrácení odkazu na okno, které vytvořilo nové okno:
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
Podpora prohlížeče
open()
je podporován ve všech prohlížečích:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |