Window open() 方法

定义和用法

open() 方法会打开新的浏览器窗口或新选项卡,具体取决于您的浏览器设置和参数值。

另请参阅:

close() 方法

实例

例子 1

在新的浏览器选项卡中打开 "www.codew3c.com":

window.open("https://www.codew3c.com");

Δοκιμάστε Εξοπλιστικά

页面下方提供更多实例。

语法

window.open(url, name, χαρακτηριστικές, replace)

参数

参数 description
url

可选。要打开的页面的 URL。

如果未指定 url,则会打开一个新的空白窗口或选项卡。

name 可选。target 属性或窗口的名称。
χαρακτηριστικές 可选。以逗号分隔的项目列表,无空格。
replace

已弃用。

规定 URL 是创建新条目还是替换历史列表中的当前条目。

支持以下值:

  • true - URL 替换历史列表中的当前文档
  • false - URL 在历史列表中创建新条目

警告:Chrome 在使用 replace 参数时会抛出异常。

name 参数支持以下值:

value description
_blank URL 被加载到新窗口或选项卡中。默认。
_parent URL 被加载到父框架中。
_self URL 替换当前页面。
_top URL replaces any frameset that may be loaded.
name the name of the window (does not specify the window title).

The features parameter supports the following values:

value description
fullscreen=yes|no|1|0 whether to display the browser in full-screen mode. The default is no. Full-screen mode windows must also be in cinema mode. Limited to IE.
height=pixels the height of the window, in pixels. The minimum value is 100.
left=pixels the left position (X coordinate) of the window, in pixels. Negative values are not allowed.
location=yes|no|1|0 whether to display the address field. Limited to Opera.
menubar=yes|no|1|0 whether to display the menu bar.
resizable=yes|no|1|0 whether the window can be resized. Limited to IE.
scrollbars=yes|no|1|0 whether to display scrollbars. Limited to IE, Firefox, and Opera.
status=yes|no|1|0 whether to add a status bar.
titlebar=yes|no|1|0 whether to display the title bar. It will be ignored unless the application called is an HTML application or a trusted dialog.
toolbar=yes|no|1|0 whether to display the browser toolbar. Limited to IE and Firefox.
top=pixels the top position (Y coordinate) of the window, in pixels. Negative values are not allowed.
width=pixels the width of the window, in pixels. The minimum value is 100.

return value

returns a reference to the new window, or null if the call fails.

technical details

description

open() method will search for an existing window or open a new browser window. If name if a window that already exists is specified, then it returns a reference to that window. The returned window will display url the document specified by the χαρακτηριστικές parameters. In the case of knowing only the window name, this is the only way JavaScript can obtain a reference to that window.

if not specified name parameters, or if the window specified does not exist, then open() method will create a new browser window. This new window will display url the URL specified by the name specified, size, and controls are determined by χαρακτηριστικές parameters specify. url is an empty string, then open() opens a new window.

name parameters specify the name of the new window. This name can only contain numbers, letters, or underscores. It can be used as a <a> and <form> the value of the target attribute, used to force the document to be displayed in this specified window.

when using the method Window.open() When loading a new document into an existing specified window, it can be passed replace παράμετρο, η οποία χρησιμοποιείται για να δηλώσει αν το νέο έγγραφο θα έχει τη δική του εγγραφή στην ιστορικό του παραθύρου ή αν θα αντικαταστήσει την εγγραφή του τρέχοντος εγγράφου. Αν replace του είναι true, το νέο έγγραφο θα αντικαταστήσει το παλιό έγγραφο. Αν η τιμή είναι false ή παραλείπεται, το νέο έγγραφο θα έχει τη δική του εγγραφή στην ιστορικό του παραθύρου. Η λειτουργία που παρέχει αυτός ο παράμετρος είναι παρόμοια με Location.replace() μεθόδου η παροχή λειτουργιών είναι πολύ παρόμοια.

μην συγχέετε Window.open() μεθόδους Document.open() μεθόδουαυτή τη συνάρτηση Window.open()αυτή τη συνάρτηση open(). Συνήθως, σε ορισμένους ορισμένους ως HTML χαρακτηριστικά εκδηλώσεων, η συνάρτηση open() ερμηνεύεται ως Document.open()είναι Window.open().

Χαρακτηριστικές παραθύρου

χαρακτηριστικές ορίζει τη λίστα χαρακτηριστικών που θα εμφανιστούν στο παράθυρο, οι χαρακτηριστικές των οποίων διαχωρίζονται με κόμματα. Αν η τιμή αυτού του προαιρετικού παραμέτρου είναι κενή ή παραλείπεται, το παράθυρο θα εμφανίσει όλες τις χαρακτηριστικές. Ωστόσο, αν χαρακτηριστικές ορίζει μια χαρακτηριστική, τότε οι χαρακτηριστικές που δεν εμφανίζονται στη λίστα δεν θα εμφανιστούν στο παράθυρο. Να σημειωθεί ότι αυτή η αλφαριθμητική δεν περιέχει καμία κενή ή χαρακτήρες διαστήματος, και η μορφή κάθε στοιχείου είναι όπως παρακάτω:

χαρακτηριστική[=τιμή]

Για πολλές χαρακτηριστικές,τιμή της είναι yes ή no. Οι χαρακτηριστικές αυτές ακολουθούνται από το ίσο και τιμή τιμές μπορούν να παραλειφθούν, αν εμφανίζεται η χαρακτηριστική, υποθέτεται η τιμή της τιμή Η τιμή είναι yes, αν δεν εμφανίζεται, υποθέτεται τιμή Η τιμή είναι no. Ωστόσο, η τιμή της χαρακτηριστικής width ή height τιμή Η τιμή είναι απαραίτητη, βεβαιωθείτε ότι καθορίσατε τις τιμές τους σε pixel.

Υποστήριξη περιηγητή

Όλοι οι περιηγητές υποστηρίζουν open():

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη Υποστήριξη

Περισσότερες παραδείγματα

παράδειγμα 2

Άνοιξτε τη σελίδα about:blank σε νέο παράθυρο/καρτέλα:

var myWindow = window.open("", "", "width=200,height=100");

Δοκιμάστε Εξοπλιστικά

παράδειγμα 3

Άνοιξτε νέο παράθυρο με το όνομα "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>");

Δοκιμάστε Εξοπλιστικά

παράδειγμα 4

με νέο παράθυρο αντικαταστήστε το τρέχον παράθυρο:

var myWindow = window.open("", "_self");
myWindow.document.write("<p>Απέplaced το τρέχον παράθυρο.</p>")}

Δοκιμάστε Εξοπλιστικά

Παράδειγμα 5

Ανοίγοντας νέο παράθυρο και ελέγχοντας την εμφάνισή του:

window.open("https://www.codew3c.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");

Δοκιμάστε Εξοπλιστικά

Παράδειγμα 6

Ανοίγοντας πολλαπλά καρτέλες:

window.open("http://www.google.com/");
window.open("https://www.codew3c.com/");

Δοκιμάστε Εξοπλιστικά

Παράδειγμα 7

Ανοίγοντας νέο παράθυρο. Μετά χρησιμοποιούμε το close() για να κλείσουμε αυτό το παράθυρο:

function openWin() {
  myWindow = window.open("", "myWindow", "width=200,height=100");   // Ανοίγοντας νέο παράθυρο
}
function closeWin() {
  myWindow.close();   // Κλείσιμο αυτού του νέου παραθύρου
}

Δοκιμάστε Εξοπλιστικά

Παράδειγμα 8

Ανοίγοντας νέο παράθυρο. Χρήση της ιδιότητας name για να επιστρέψουμε το όνομα του νέου παραθύρου:

var myWindow = window.open("", "MsgWindow", "width=200,height=100");
myWindow.document.write("<p>Το όνομα αυτού του παραθύρου είναι: " + myWindow.name + "</p>");

Δοκιμάστε Εξοπλιστικά

Παράδειγμα 9

Χρήση της ιδιότητας opener για να επιστρέψουμε την αναφορά στο παράθυρο που δημιουργήθηκε

var myWindow = window.open("", "myWindow", "width=200,height=100");   // Ανοίγοντας νέο παράθυρο
myWindow.document.write("<p>Αυτό είναι 'myWindow'</p>");   // Κείμενο στο νέο παράθυρο
myWindow.opener.document.write("<p>Αυτό είναι το πηγαίο παράθυρο!</p>");  // Δημιουργία νέου παραθύρου με το κείμενο

Δοκιμάστε Εξοπλιστικά