Tải bản đầy đủ - 0 (trang)
Browser control: Controlling the window's size and location

Browser control: Controlling the window's size and location

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

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);



260



Find the interactive coding exercises for this chapter at:

http://www.ASmarterWayToLearn.com/js/80.html



261



81

Browser control:

Testing for popup blockers

Popup blockers are now a standard feature of browsers, with some level of popup

blocking usually built in as a default. JavaScript popups, and especially those that open a new

page within the same website, are often tolerated by default, but you can never be sure. If

popups are an essential feature of your site, you need to test whether your popups are going to

be blocked. If they are, you can ask the user to disable the popup blocker for your site.

The test is pretty inelegant. You attempt to open a small test popup, then quickly close it

—so fast that the user may not notice the little screen flash. If the attempt succeeds, the

window's handle will have a value. If the popup is blocked, the handle will be null, and you

can deliver your message to the user.

Here's the code.

1 function checkForPopBlocker() {

2

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

3

if (testPop === null) {

4

alert("Please disable your popup blocker.");

5

}

6

testPop.close();

7 }



Here's the line-by-line breakdown:

2 Attempts to open a tiny window

3-4 If the handle is assigned null, meaning the window couldn't open, an alert displays

6 Closes the window

The function shown above works with all browsers except Internet Explorer. In Internet

Explorer, if the popup is blocked, the handle will be undefined instead of null. So to cover all

bases, you need to code the function this way.

1 function checkForPopBlocker() {

2

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

3

if (testPop === null || typeof(testPop === "undefined") {

4

alert("Please disable your popup blocker.");

5

}

6

testPop.close();

7 }



Normally, you'd run the test when the page loads.





262



Find the interactive coding exercises for this chapter at:

http://www.ASmarterWayToLearn.com/js/81.html



263



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

Browser control: Controlling the window's size and location

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

×