Tải bản đầy đủ - 0 (trang)
Browser control: Filling the window with content

Browser control: Filling the window with content

Tải bản đầy đủ - 0trang

content. You could also use it to write to a window that already has content. But it will

overwrite all the HTML of the original document, replacing its content entirely.

Instead of assigning the HTML string to a variable and specifying the variable inside the

parentheses, you could just put the HTML string inside the parentheses, enclosed in

quotes of course. But this would be even more unwieldy than the example code.

The second way to fill the window with content is to assign a document to it, as you

learned to do in previous chapters.



monkeyWindow.location.href = "http://www.animals.com/capuchin.html";

The third and most common way to fill the window with content is to include the

document assignment in the statement that opens the window.

var monkeyWindow = window.open("http://www.animals.com/capuchin.html");

If the document you're opening in the popup shares the same host and directory as the

original document, you can just write...

var monkeyWindow = window.open("capuchin.html");

This is how you close a window.



Find the interactive coding exercises for this chapter at:




Browser control:

Controlling the window's size and location

In the last chapter you learned to open a new window without parameters—a full-size


You also learned to open a window with a single parameter—a URL.

A second parameter that you can include is a window name.

var monkeyWindow = window.open("monk.html", "win1");

In the statement above, the second item in the parentheses, "win1", is the name.

The name is useful for specifying the target attribute of an or
element in


Things to know:

The name, used in HTML, is not the handle. The handle is used in JavaScript statements

that write to the window, assign a URL to it, or close it. The handle is the variable that

precedes the equal sign.

The name is in quotes.

The name is separated from the URL by a comma followed by a space.

The name itself can't have spaces in it.

You can specify a URL parameter without a name parameter, but you can't specify a name

parameter without a URL parameter. But it is okay to specify an empty URL parameter, like


var monkeyWindow = window.open("", "win1");

Often, you'll want to specify a window size.

var monkeyWindow = window.open("monk.html", "win1", "width=420,height=380");

Things to know:

Both parameters, width and height, are enclosed by a single set of quotation marks.

The absence of spaces within the quotation marks isn't a mere style preference but a

requirement. Any spaces here will break JavaScript.

The numbers refer to pixels. In the example above, the window will be 420 pixels wide


and 380 pixels high.

Width and height must be a minimum of 100.

Unlike the URL and name parameters, the order doesn't matter. Width can come before

height, height can come before width. But the width-and-height set must come third, after

URL and name.

In order to specify these parameters, you must specify a URL and name, even if you

specify empty strings.

A window that's smaller than the screen will display in the upper-left corner of the

screen. But you can optionally tell the browser where to place the window.

var w = window.open("", "", "width=420,height=380,left=200,top=100");

Things to know:

Again, the numbers are pixels—number of pixels from the left edge of the screen and

number of pixels from the top of the screen.

The positioning parameters are included within the same set of quotation marks as the

size parameters, and, like the size parameters, are separated by a comma and no space.

The parameter order within the quotation marks doesn't matter.

You can specify window size without specifying window position, but if you specify

window position without size, it will be ignored since it will be a full-size window that

fills the whole screen.

As usual, some or all of the parameters can be assigned to a variable, and the variable

can be used in the statement that opens the window. Since the whole thing has to be a quoted

string, the quotes within the string have to be changed to single quotes.

1 var windowSpecs = "'faq.html', 'faq', 'width=420,height=380,left=200,top=100'";

2 var faqPage = window.open(windowSpecs);


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Browser control: Filling the window with content

Tải bản đầy đủ ngay(0 tr)