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

Taking Your Talent to the Web

321

Recognize that developers bash their brains out writing code like this because browsers behave so inconsistently from version to version and platform to platform. Be glad you’re going into web design and not web development. Be kind to your programmers.

On the off-chance that you find this stuff enthralling or decide to switch from design to development, you’ll find an abundance of good browser detection information at http://webreference.com/tools/browser/ javascript.html and http://developer.netscape.com/viewsource/ krock_v5.html. Unfortunately, there is always the chance that by the time you read this book, these pages will have moved or disappeared. If so, check the Resources Department at http://www.webstandards.org/ for the latest on browser detection.

GOING GLOBAL WITH JAVASCRIPT

Just as with style sheets (Chapter 10), it is possible and often desirable to save time, hassles, and bandwidth by creating one or more global JavaScript documents, which can then be used to control whole sections of your site—or even the entire site.

For instance, the “My Glamorous Life” section at zeldman.com (http:// www.zeldman.com/glamorous/) is controlled by a single JavaScript document (http://www.zeldman.com/glamorous/glam.js).

The document, in its entirety, reads as follows:

// Menubar preload. Pretty standard stuff. function newImage(arg) {

if (document.images) { rslt = new Image(); rslt.src = arg; return rslt;

}

}

function changeImages() {

if (document.images && (preloadFlag == true)) {

for (var i=0; i<changeImages.arguments.length; i+=2) { document[changeImages.arguments[i]].src = changeImages.arguments[i+1];

}

322 HOW: The Joy of JavaScript: Going Global with JavaScript

}

}

var preloadFlag = false; function preloadImages() {

if (document.images) {

tocover = newImage(“../omen2/coreover.gif”); funover = newImage(“../omen2/funover.gif”); alaover = newImage(“../omen2/alaover.gif”); 15over = newImage(“../omen2/15over.gif”); stealover = newImage(“../omen2/stealover.gif”); webover = newImage(“../omen2/webover.gif”); miscover = newImage(“../omen2/miscover.gif”); comingover = newImage(“../glareon.gif”); preloadFlag = true;

}

}

//Get out of some idiot’s frame.

if (top != self) { top.location = self.location; }

//Popup window, 640 x 480

function open_window6(url) {

mywin = window.open(url,”win”,’toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=640,height=480’);

}

// Popup window, 500 x 500 function open_window(url) {

mywin = window.open(url,”win”,’toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=500,height=500’);

}

Pretty “light” after all that stuff from Juxt Interactive, eh? By now it should be obvious what this stuff means, but we’ll spell it out anyway because we really, truly love you.

The double slashes // precede comments. The comments help the author remember what each function is for. The double slashes tell the browser to ignore these comments and proceed to the next function.

The menu bar preload and subsequent changeImages function are just another way of preloading images and creating image rollovers. The images in this case are referenced via relative URLs (../glareon.gif), as explained in Chapter 8. It would have been smarter to use absolute URLs, but we never claimed to be all that bright.

Taking Your Talent to the Web

323

Get out of some idiot’s frame is a simple framebuster script, consisting of

just one line.

if (top != self) { top.location = self.location; }

A third-party site might link to yours. Sometimes that third-party site uses frames. Sometimes those frames are poorly constructed. Your site might load inside their frames instead of in its own window. This line of JavaScript prevents that from happening. In English, what it is saying is, “The HTML document referenced by this script should fill the browser window. If it does, swell. If it doesn’t, get rid of any extraneous frames and fill the browser window with our page, not some other jerk’s.” Of course JavaScript syntax is a bit more formal than that.

The subsequent two functions are pop-up windows of varying dimensions. They are identical except for their dimensions and their names. (The 640 x 480 window is named window6; the other is simply named window.) The parenthetical URL (url) is a variable. If a pop-up window is needed on any HTML page that refers to this global JavaScript document, the address of the pop-up window will be inserted between the parentheses (popupwindow.html).

How do the HTML pages make use of this global JavaScript document? Just as with global style sheets, they do it by referring to the .js file with a link:

<script “”type=”text/javascript” src=”glam.js”></script>

The link appears inside the <HEAD> of each HTML document that requires these scripts.

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd”> <html>

<head>

<link rel=”StyleSheet” href=”glam.css” type=”text/css” media=”screen”> <script “”type=”text/javascript” src=”glam.js”></script>

<title>Jeffrey Zeldman Presents: My Glamorous Life</title> </head>

<body onLoad=”preloadImages(); window.defaultStatus=’Jeffrey Zeldman Presents.Entertainment, free graphics, and web design tips since 1995.’”>

324 HOW: The Joy of JavaScript: Learning More

Notice that the <BODY> tag includes these two onLoad functions: preloadImages and window.defaultStatus. The first preloads the images as referenced in glam.js. The second is our old friend, the default status bar message—the first snippet of JavaScript we learned in this chapter. The two are combined in one onLoad declaration and separated by a semicolon. Simple.

LEARNING MORE

There is so much that JavaScript can do. This chapter barely hints at the possibilities, and some methods used in this chapter could be out of date by the time you read this book.

With the arrival of full support for ECMAScript and the DOM, the dynamic possibilities for websites will expand exponentially. If you find, as some do, that you take naturally to JavaScript and want to learn more about the standardized version of JavaScript (ECMAScript) and the DOM:

The W3C offers the DOM at http://www.w3.org/DOM/ in all its baffling glory.

WebReference’s “Doc JavaScript” (http://www.webreference.com/ js/) offers many fine articles covering ECMAScript, JavaScript, and the DOM.

Peter-Paul Koch maintains a DOM mailing list (http://www.xs4all.nl/ ~ppk/js/list.html).

The Web Standards Project maintains links to the latest ECMAScript and DOM resources, beginning at http://www.webstandards.org/ resources.html.

And A List Apart (http://www.alistapart.com/) offers the Eisenberg DOM series, an ongoing tutorial that includes:

Meet the DOM: http://www.alistapart.com/stories/dom/

DOM Design Tricks: http://www.alistapart.com/stories/dom2/

DOM Design Tricks 2: http://www.alistapart.com/stories/domtricks2/

DOM Design Tricks 3: http://www.alistapart.com/stories/domtricks3/

Taking Your Talent to the Web

325

Whether you tackle this advanced stuff now or crawl off to recover from reading this chapter, be proud of yourself. You have faced your fears and at least looked at the part of web design that most designers find confusing and unintuitive. This is mainly because, compared to Photoshop and <p> paragraph tags, JavaScript is confusing and unintuitive.

But with practice and experience, it will get easier. And when browsers do a better job of complying with ECMAScript and the W3C DOM, it will get easier still. The programming will not be easy, but you or your development team will take comfort in the fact that you only have to code your site one way to work in all browsers.

There is just a little more to learn before you can consider yourself a full-

fledged (or at least a fledgling) web designer. And by a strange coincidence, what you still don’t know is covered in the very next chapter. Let’s go for it, shall we?

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