ウィンドウopen()
例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 |