Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Taking_Your_Talent_to_the_Web.pdf
Скачиваний:
5
Добавлен:
11.05.2015
Размер:
9.91 Mб
Скачать

Taking Your Talent to the Web

307

remain accessible to those who surf with images turned off or who are using nongraphical browsers such as Lynx. The link to /main.html will work even if JavaScript has been turned off in the user preferences (or the browser does not support JavaScript).

The code and the effect on the web page are much simpler than the descriptive text you’ve just waded through.

You might ask, can JavaScript text rollovers be added to an image rollover like the one just described? The answer is yes, and it can be done very easily:

<a href =”/main.html” onMouseOver=”swapem(main, mainover); window.status=’Visit themain page.’; return true;” onMouseOut=”swapem(main, mainout); window.status=’’;return true;”><img name=”main” src=”/images/menubar_out_1.gif” width=”200”height=”25” border=”0” alt=”Visit the main page.” title=”Visit the main page.”></a>

WINDOWS ON THE WORLD

Problem: The site offers streaming video files. You, the client, or the information architect want these files to play back inside the browser via the QuickTime plug-in (see Chapter 12). It is easy to use the HTML <EMBED> or <OBJECT> tags to embed a QuickTime movie in a thoughtfully designed HTML page. But if you do this on the current page, the movie will begin streaming even if visitors do not have the bandwidth or patience to see it.

Solution: The JavaScript pop-up window.

Opening new windows via JavaScript is a simple task, though it’s somewhat controversial. Some web users feel that everything should happen in their existing browser window. These folks hate pop-up windows, remote controls, and everything else that can happen outside the safe, familiar world of their existing browser window.

Are these users right? They are right for themselves.

What does this mean? It means that pop-up windows, remotes, and other such stunts should never be created lightly or purposelessly. (Why offend visitors if you can avoid it?)

308 HOW: The Joy of JavaScript: Windows on the World

Figure 11.4

JavaScript pop-up windows annoy some web users but can be extremely functional. At TV Guide’s site, the main page offers a compressed listing of all available cable channels. Clicking any program triggers a pop-up window that offers detailed information about the selected show. Here, for instance, we can read about Dick Shawn groping for laughs as a drunken genie in The Wizard of Baghdad. The point is that JavaScript allows the user to select exactly the level of

detail needed (www.tvguide.com).

Sometimes, however, you need pop-up windows. Sometimes, nothing else will do—as in the present example, when you wish to embed a streaming video file in a web page but don’t want to force that streaming movie on users who don’t care to (or can’t) view it. Pop-up windows can also be used to provide additional information as needed (see Figure 11.4). In case of emergency, break glass and use JavaScript to easily create new windows.

Get Your <HEAD> Together

Before you can create a new window, you must define it in the HTML <HEAD> of your HTML document.

Here is a typical way to do just that:

<html>

<head>

<title>Welcome to Porkchops.com!</title>

Taking Your Talent to the Web

309

<script type=”text/javascript”> <!--

function awindow(url) {

return window.open(url, “thewindow”, “toolbar=no,width=350,height=400,status=no,scrollbars=yes,resize=no,menubar=no”);

}

// --> </script> </head>

What are we doing? We have defined a function, given it a name (aWindow), and defined its properties: It will not have a toolbar (toolbar=no), it will be 350 pixels wide (width=350), it will stay the exact size we’ve specified (resize=no), and so on.

We have also, without even realizing it, declared a JavaScript variable—that is, an element that can be replaced, as in the swapem example. Our variable is the URL of any HTML document we would like to use in the pop-up window.

In the HTML page, we would trigger the function like so:

<a href=”sucky_old_browser.html” onClick=”aWindow(‘porkpops.html’); return false;”>

When the event is triggered by the user’s action (clicking the link), the named window.open function will be performed, and the appropriate HTML page will appear in a 350 x 400 pop-up window with no status bar or menu bar. The return false will prevent the browser from following the URL specified in the <HREF>, for backward compatibility.

As a courtesy, it’s nice to include a <CLOSE WINDOW> function in the popup window itself, for the beginners in our viewing public. Porkpops.html should include a link like this:

<a href=”#” onclick=”window.close(); return false;”>Close me!</a>

Onclick is another of those essential built-in JavaScript event handlers you’ll come to know and love, and window.close is a built-in JavaScript function that, as you might have guessed, closes windows. In other words, we are telling the browser to close the window—pretty basic stuff.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]