window.alert('Hello! This is my message to you!');
We can also use this method as an event handler:
| Code | Result |
|---|---|
<form>
<input type=button value="Alert"
onClick="alert('This is my second message.\nIt contains two lines.');" >
</form>
|
Method confirm() allows us to ask user a true/false question. This method takes one string argument and creates a window with this message and two buttons OK and Cancel. This method returns true if user clicked OK button and false otherwise. Here is an example:
| Code | Result |
|---|---|
function submitting()
{
var msg = "You left some fields of the form empty. "+
"Are you sure you want to submit the application?";
if( confirm(msg) )
alert('You want to submit!');
else
alert("Let's get back to the form then");
}
...
<input type=submit onClick="submitting();">
|
Method prompt() is the next step in asking user questions. This methods allows to ask a question and get a string as an answer. This method takes two arguments:
| Code | Result |
|---|---|
function get_name()
{
var name;
name = prompt('Please enter your name', '');
if( name )
alert('You entered "'+name+'"');
else
alert('You did not type any name or pressed Cancel to close the window');
}
...
<form>
<input type=button value="Ask my name" onClick="get_name();">
</form>
|
Method open() opens a new browser window. This method can take up to four arguments, however, none of them is required. These arguments are:
| Element | Value | Description |
| height | pixels | Sets the initial height of the new window. |
| width | pixels | Sets the initial width of the new window. |
| top | pixels | Sets the Y-coordinate of the top-left angle of the new window. |
| left | pixels | Sets the X-coordinate of the top-left angle of the new window. |
| directories | yes|no or 1|0 | Display or hide the directories in the new window. |
| location | yes|no or 1|0 | Display or hide the location bar in the new window. |
| menubar | yes|no or 1|0 | Display or hide the menu in the new window. |
| resizable | yes|no or 1|0 | Allow the user to resize the new window or prohibit resizing. |
| scrollbars | yes|no or 1|0 | Display or hide the scroll bar of the new window. |
| status | yes|no or 1|0 | Display or hide the status bar in the new window. |
"feature1=value1; feature2=value2; ..."Example:
window.open('mypage.html', 'win1',, "status=no; menubar=no; scrollbars=yes");
The following example allows you to open windows with different parameters.
The easiest and probably the most useful is method close() that closes the window which calls is. Here is a small example where in the first line we open another window and memorize the reference to the new window in variable new_win. In the second line we close the window by using method close() called as method of new_win object:
Method moveTo() takes two integer arguments top and left and moves the window in such a way that the top left corner of the window is located at the point with coordinates top and left. The following example moves the window created in the previous example in the position with coordinates 100 and 200:
Similarly method moveBy() takes two integer arguments shift_right and shift_bottom and moves the window by the given shifts from its current position. This method can also take negative arguments to move the window up and/or left:
Methods resizeTo() and resizeBy() allow us to change the size of the window. The first of these methods sets a new window sizes, which are specified as arguments:
The other method changes the current size of the window by the arguments specified:Two more methods scrollTo() and scrollBy() allow us to scroll the window.
![]() |
Notice: if you want to change size, scroll, move, or close the current window you should use methods of this
window; that is, |
One more useful method of the Window object is method focus(). This method brings the window called the method up front. This can be used when you work with several windows and want to show a window hidden behind the others:
Using JavaScript we can control the current window and any child window we created in this script. An interesting question is whether a child window can control the parent window. The answer is yes. However, to do so the child window needs to have a reference to the window, which opened this child. This reference is to be kept in property opener of the Window object. A web page designer should take care about assigning a proper value to that property manually, though. Usual procedure of opening a new window looks like shown in the example below:
var child_win = null;
child_win = window.open(url, name, features);
if( child_win != null ){
new_win.opener = window;
...
}
else{ // an error happened. Browser couldn't open a new window
alert('Error: cannot open a new window.');
...
}
This example shows how two windows parent and child can control
each other.
/* add the header to the current document */
document.write('<h2>This header is generated by JavaScript</h2>');
// add an image to the child window
child_win.document.writeln('<img src="../images/excl.gif">');
The only difference between these two methods is that method writeln() adds a new line symbol '\n'
at the end of the line.
Method close() (do not be confused with method close() of Window object) closes the document for input; that is, no more lines can be added to it. This method is useful in a situations like the one described below. Assume we need to open a window named my_new_win and add several lines to it:
If you click on the text of the example, the code shown will be executed and you'll see a new window created with two headers in it. Now let's click there one more time. What's going to happen? Since a window with name my_new_win already exists the browser will not recreate it. Since method close() hasn't been deployed to the document in the new window we are still allowed to add lines there. Thus, the same two headers will be added to the window. Now let us try the same code with the calling method close() at the very end. Now the second execution of the same code rewrites the content of the document in the window.