Optimizing WebPage (Sybex)
Optimizing WebPage (Sybex)
Optimizing WebPage (Sybex)
HTML Complete
Sybex, Inc.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved. No part of this
publication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to
photocopy, photograph, magnetic or other record, without the prior agreement and written permission of the publisher.
ISBN: 0-7821-4209-5
SYBEX and the SYBEX logo are either registered trademarks or trademarks of SYBEX Inc. in the USA and other
countries.
TRADEMARKS: Sybex has attempted throughout this book to distinguish proprietary trademarks from descriptive terms
by following the capitalization style used by the manufacturer. Copyrights and trademarks of all products and services
listed or described herein are property of their respective owners and companies. All rules and laws pertaining to said
copyrights and trademarks are inferred.
This document may contain images, text, trademarks, logos, and/or other material owned by third parties. All rights
reserved. Such material may not be copied, distributed, transmitted, or stored without the express, prior, written consent
of the owner.
The author and publisher have made their best efforts to prepare this book, and the content is based upon final release
software whenever possible. Portions of the manuscript may be based upon pre-release versions supplied by software
manufacturers. The author and the publisher make no representation or warranties of any kind with regard to the
completeness or accuracy of the contents herein and accept no liability of any kind including but not limited to
performance, merchantability, fitness for any particular purpose, or any losses or damages of any kind caused or alleged
to be caused directly or indirectly from this book.
Sybex Inc.
1151 Marina Village Parkway
Alameda, CA 94501
U.S.A.
Phone: 510-523-8233
www.sybex.com
Chapter 13
Optimizing Your Web Pages
for Internet Explorer
n Chapter 6, Planning for a Usable, Maintainable Website,
you learned about site planning and site maintenance. Another
important step in the site planning process is to evaluate your
audience and the browsers your audience is most likely to use
to view your web pages. This gives you valuable information for
making decisions about which features you should include in
your pages for the best viewing experience by your target audience.
In this chapter, youll learn the major features of Internet
Explorer 6, the latest version of IE, including its support for
technologies, plug-ins, and a variety of scripting, markup, and
programming languages. In the next chapter (Chapter 14,
Optimizing Your Web Pages for Netscape Navigator), youll
learn about Netscape Navigator 6 and 7.
Part III
420
Chapter Thirteen
WARNING
Internet Explorer 6 currently is available only for PCs. The latest versions
of Internet Explorer for the Mac include version 5.1.6 for Mac OS 8.1 and 9.x
and version 5.2.2 for Mac OS X.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
421
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
Web Standards
422
Chapter Thirteen
web standards, the latest browsers (Internet Explorer 6 and Netscape 6
and 7) support the W3C specifications for HTML 4.01, XHTML 1.0,
XML 1.0, ECMAScript 262 (the standard version of JavaScript), Cascading Style Sheets level 1 (CSS1), and most (but not all!) features of
Cascading Style Sheets level 2 (CSS2).
The browser manufacturers are not the only ones that need to address
the issue of compliance with current web standards. Web designers and
developers also must learn to design and create web pages that comply with
web standards. This is not only economical; it also ensures that pages
created today will continue to be accessible in the browsers of tomorrow.
To learn more about web standards:
8 See the article by Paul Boutin, Web Standards for Hard Times, at
http://hotwired.lycos.com/webmonkey/02/33/index1a.html.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
FIGURE 13.2:
Part III
FIGURE 13.1:
423
TIP
Both CSS and XSLT (Extensible Style Language Transformations) style sheets
can be used to display XML content as HTML and XHTML pages.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
424
Chapter Thirteen
NOTE
To learn more about the DOM and scripting for browsers, see Chapter 18,
Bringing Pages to Life with Dynamic HTML and XHTML.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
425
This DOCTYPE specifies that the document has a root element of html, uses
the elements and attributes of XHTML 1.0, does not include presentational
markup (strict), and is in the English language.
See Chapter 3 for more information about using DOCTYPE declarations in HTML
and XHTML documents.
The DTDs are included in the W3C specifications and can be viewed at
the W3C site (www.w3.org). Although a DOCTYPE declaration is not required
for an HTML or XHTML page to be displayed in a browser, the newer
browsers (6 and above) use the DOCTYPE declaration to make decisions
about how the page should be displayed.
In Internet Explorer 6, if no DOCTYPE declaration is included, the page
will be displayed in what is called backward-compatibility or quirks
mode, which means that the page will be displayed in the same way as it
would in earlier versions of Internet Explorer (before version 6). If a DOCTYPE
declaration is included, Internet Explorer 6 will do its best to display the
page according to the rules of the specified DTD.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
TIP
426
Chapter Thirteen
NOTE
Browsers that use DOCTYPE to determine how a page is displayed include Internet Explorer 6 and above on Windows, Internet Explorer 5 and above on Macs,
and all Mozilla-based browsers, including Netscape 6 and above.
WARNING
Internet Explorer 6 will not work in Windows 95 or earlier versions of the
Windows operating system.
FIGURE 13.3:
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
427
To disable the image toolbar for a specific image, add the galleryimg
attribute to the img element, as in the following:
<img src=cat.gif alt=tabby cat height=250 width=250
galleryimg=no />
Part III
FIGURE 13.4:
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
428
Chapter Thirteen
TIP
For more information on configuring the new features of Internet Explorer 6,
see Customizing New Browser Features at www.microsoft.com/windows/
ie/using/howto/default.asp.
TIP
To learn more about including multimedia in your web pages, see Chapter 15.
The media bar settings can be accessed by clicking the Media icon
on the Internet Explorer 6 toolbar or by choosing View Explorer Bar
Media. The media bar (see Figure 13.5) will also appear the first time a
user clicks on a media file link on a page in Internet Explorer 6. The
first time the media bar appears, the user is offered the option to play
media files in Internet Explorer. Those settings also can be accessed
at any time by clicking the down arrow labeled Media Options in the
media bar.
The media bar is enabled by default. To disable the media bar in your
browser, choose Tools Internet Options from the Internet Explorer
menu bar, click on the Advanced tab, and check (disable) the Dont
Display Online Media Content in the Media Bar, as shown previously in
Figure 13.4.
You can use scripting to access the mediaBar object and its properties
to control the playback, display, and HTML content of the media bar. You
also can format links to media files that specify that the files wont play
in the browser window, regardless of the users media preference settings.
When the user clicks on this media file link, the file will open in a separate
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
429
NOTE
To learn more about scripting for the media bar, see Using the Media Bar in
Internet Explorer at http://msdn.microsoft.com/library/default
.asp?url=/nhp/default.asp?contentid=28000441.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
FIGURE 13.5:
430
Chapter Thirteen
Internet Explorer 6 allows the user to set preferences for cookies. It
includes six levels of preferences, including Accept All Cookies, Reject All
Cookies, and four choices in between. The browser monitors all incoming
cookie traffic based on the users chosen privacy level. To access the privacy
settings, choose Tools Internet Options from the Internet Explorer
menu bar and then click on the Privacy tab. Figure 13.6 shows details of
the Medium High privacy setting. As you can see at the bottom of the
figure, the user also can choose to override the chosen setting for individual
websites.
FIGURE 13.6:
How does this affect you as the web developer? Websites that distribute
cookies and want to be displayed in Internet Explorer 6 need to create a
privacy profile and save it as an XML file named p3p.xml that resides in
the root directory of the site. When an Internet Explorer 6 user visits the
site, the browser reads the p3p.xml file for that site and compares it to
the users chosen privacy preferences. The browser then allows the cookie
to be served if the sites privacy profile is compatible with the users privacy
preferences or alerts the user if it is not compatible.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
431
NOTE
For additional information on creating privacy profiles for your websites, see
the W3Cs Resources for P3P Implementation at www.w3.org/P3P/
develop.html and the P3P 1.0 specification at www.w3.org/TR/P3P/.
8 Making sure that the code you write is clean and clear and takes
the minimum time to load into the browser.
Adding Functionality
This is the most exciting aspect of optimization, so this is where well
begin. Adding functionality to a web page isnt as complicated as it sounds.
You dont need to write pages of HTML or learn complicated scripting
languages. Adding functionality generally means only making small
adjustments or tweaks to the HTML or XHTML and letting the browser
do the rest.
Lets begin by looking at how to optimize the layout of the page.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
432
Chapter Thirteen
Page Layout
Figure 13.7 shows a web page in Internet Explorer 6 containing a couple
of paragraphs of text. Notice the margins around the page. These are set
to the default of the browser.
FIGURE 13.7:
However, you can easily change the margins of the page to any number
of pixels you want by using the topmargin and leftmargin attributes of
the body element.
<body topmargin=0 leftmargin=0>
FIGURE 13.8:
The same page in IE 6 with top and left margins set to zero
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
433
If you wanted to set a large margin at both the top and left side of the
page, you would set them as follows:
<body topmargin=50 leftmargin=50>
TIP
Neither Netscape Navigator nor Opera supports the use of the topmargin or
leftmargin attribute of the body element and will display the page with
default margin settings if those attributes are used. Both topmargin and
leftmargin are proprietary Internet Explorer attributes that continue to be
supported in Internet Explorer 6.
TIP
If your page is created in frames (see Chapter 8, Dividing a Window with
Frames), you can use marginheight = 0 and marginwidth = 0. These are
valid HTML 4.01 attributes for the frame and iframe elements. If you are using
CSS (Chapter 16), you can use body {margin: 0;}.
Once upon a time, that blue underlined text of a hyperlink was unmistakable. Nowadays, with so much color and excitement on Web
pages, hyperlinks can blend too easily into the background. When you
want them to stand out, one of the best ways is to use colorbut not a
single color! Its far better to make them change color as the visitor moves
the mouse pointer over them.
The great thing about this is that it is easy to do, and you need to code
it only once on the page for all hyperlinks on that page to behave in the
same way.
To perform this magic, youll use a web technology called Cascading
Style Sheets (CSS). Internet Explorer 6 supports the hover pseudo-class
from the CSS level 2 specification, which is the link pseudo-class well
explore in this section.
NOTE
Cascading Style Sheets are covered in greater depth in Chapter 16.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
434
Chapter Thirteen
Lets start with a web page that has three hyperlinks. The code is
shown in Listing 13.1, and the page is shown in Figure 13.9.
Listing 13.1: A Web Page with Three Hyperlinks
<!DOCTYPE html PUBLIC
-//W3C//DTD XHTML 1.0 Transitional//EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
<title>Links</title>
<meta http-equiv=Content-Type
content=text/html; charset=iso-8859-1 />
</head>
<body>
<h1>Links, links, links!</h1>
A link to <a href=mypage.htm>Me</a>!
<br />
A link to <a href=friends.htm>my friends</a>!
<br />
A link to <a href=pets.htm>my pets</a>!
</body>
</html>
FIGURE 13.9:
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
435
Its quite easy to make these hyperlinks much more visually appealing.
Lets look at the steps:
1. First, add a style block to the head element on the web page.
This is where the Cascading Style Sheet information will go
and consists of the <style> opening and closing tags.
<title>Links</title>
<style type=text/css>
</style>
</head>
2. Next, add XHTML comment tags inside the style block that
you just created. These comment tags are put there in case
someone visits the page with an older browser that isnt capable of understanding the Cascading Style Sheet information
you will add. With comment tags in place, older browsers will
simply ignore anything between the <style> tags. Without
the comment tags, those browsers may throw out error messages or display the page incorrectly.
TIP
<title>Links</title>
<style type=text/css>
<!--->
</style>
</head>
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
It is a good idea to always add the comment tags, even if you know that the
browser your visitor is using can understand CSS.
436
Chapter Thirteen
4. Save the web page and view it in Internet Explorer 6. Move
the mouse pointer over the hyperlinks, and watch them
change color. The hyperlinks should turn red, according to
the code above. As the mouse pointer moves off them, they
automatically change back to the original color.
5. If you dont like red, you can choose another color.
<title>Links</title>
<style type=text/css>
<!-a:hover { color: teal }
-->
</style>
</head>
Once you are more familiar with other CSS properties, you can do a lot
more with hyperlinks, such as making them bold or applying an overhead
line when the mouse pointer is moved over them.
NOTE
Netscape added support for the CSS level 2 a:hover pseudo-class in Netscape 6.
437
The W3C offers free validation services for HTML and XHTML and for
CSS. Lets test the document from the previous section in the W3C XHTML
validator at http://validator.w3.org/. The heading on the page says
W3C HTML Validation Service, but it now can validate XHTML documents as well as HTML documents. If your page is available on the
web, you can enter the URL and click the button labeled Validate This
Page. If your page is not yet available online, you can upload the file
from your computer to the validator at http://validator.w3.org/fileupload.html.
Lets use the code from Listing 13.1 and see if our page validates as
XHTML:
1. Type the code in a text editor such as Notepad or SimpleText
and save the file as pseudoclass.html. This file includes an
XHTML 1.0 Transitional DOCTYPE declaration, so it will be
validated as an XHTML 1.0 Transitional document.
3. Check the box for Show Source Input. This option shows
the source code for your document as part of the validation
result. It can be very useful to have the source code at hand
when you are evaluating any error messages from the
validator. You dont need to specify a Document Type
because you have included a DOCTYPE declaration in
the document.
4. Click the button labeled Validate This Document.
Figure 13.10 shows the validation result. The source code (not seen
in the figure) is listed below the results. If we choose, we now can include
the W3C graphic that indicates that this page has been tested and validates
as XHTML 1.0.
Of course, we knew that our code was valid, so lets test our file after
we insert an error. Since we are using XHTML, we used the format <br />
for the line break element. Change one of the <br /> tags to <br>, save
the file, and revalidate it. Figure 13.11 shows the result. This time, the
validator has pointed out the invalid form of the <br> tag for an XHTML
document.
As you can see, using a validator is a free and easy way to test your
pages for compliance with web standards, and it is also a great way to
learn web standards.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
438
Chapter Thirteen
FIGURE 13.10:
FIGURE 13.11:
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
439
The Web Design Group also offers a free validation service for HTML
and XHTML documents at www.htmlhelp.com/tools/validator/. The
site also includes many support documents for using the validator. The
error message text generated by the Web Design Group validator generally is
much more helpful than the sometimes cryptic error messages generated
by the W3C validator.
With improved compliance with web standards in the new browsers, directing users to a specific version of a page should be a rare event! However,
Internet Explorer and Netscape Navigator, although certainly more alike
than in the past, still differ. Even if your pages are compliant with web
standards, those standards may not yet be supported in both Internet
Explorer and Netscape Navigator. Making separate versions of site pages
is costly, but occasionally it still is necessary. If you have had to create separate versions of web pages for different browsers, consider implementing
JavaScript to detect which browser your users are using. Remember, if
users are not using the browser for which the documents were developed,
theyll get error messages.
Browser detection in the past generally was a simple matter of determining whether users were using Netscape Navigator or Internet Explorer,
using two simple JavaScript methods and properties:
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
440
Chapter Thirteen
following simple script, you can find out the userAgent string returned
by your current browser:
<script type=text/javascript language=javascript>
var agent = navigator.userAgent;
document.write (userAgent string: + agent);
</script>
Here are a few examples of the userAgent string returned by this script:
userAgent string: Mozilla/4.75 [en] (Win98; U)
userAgent string: Mozilla/3.0 (compatible; Opera/3.0;
Windows 95/NT4)
userAgent string: Opera/6.05 (Windows XP; U) [en]
userAgent string: Mozilla/5.0 (Windows; U; Win98; en-US;
CDonDemand) Gecko/20010131 Netscape6/6.01)
userAgent string: Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1)
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
441
if it is a different browser from the one you are expecting, redirect the user
to the appropriate web page suited to his browser.
The code that you will use, called a script, is written in a scripting language called JavaScript. If youre now beginning to worry because you
havent used a script before, dont. The great thing about this script is
that you dont need to know how it works. You just need to add it to the
web page and watch it work.
WARNING
According to November 2002 statistics at www.thecounter.com, 10 percent
of web users have JavaScript turned off in their browsers.
The JavaScript we are going to add will be inserted inside the <head>
tag, after the <title> tag. The script is as follows:
<script type=text/javascript language=JavaScript>
<!-var browser = unknown
var version = 0
var detected = false
var ua = window.navigator.userAgent
if (ua.substring(0,7) == Mozilla) {
if (ua.indexOf(MSIE) > 0) {
browser = Microsoft
}
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
Lets start off with an empty XHTML page and look at how to add the
script to it. Heres the script for the empty page:
442
Chapter Thirteen
if (ua.indexOf(6.) > 0) {
version = 6
}
}
//Find Internet Explorer 6 users and do nothing
if (browser == Microsoft && version == 56) {
detected = true
}
//Find other JavaScript-enabled browser here and move
//them to another, more suitable page!
if (detected == false) {
location.href = alternate.html
}
//-->
</script>
443
browser = Microsoft
}
if (ua.indexOf(6.) > 0) {
version = 6
}
}
//Find Internet Explorer 6 users and do nothing
if (browser == Microsoft && version == 6) {
detected = true
}
Note that we have used different background colors and have also
included different messages in the body section of these pages to tell
them apart as we test our code. Figure 13.12 shows the page displayed
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.
Part III
444
Chapter Thirteen
when browser_detect.html is opened in Internet Explorer 6, and
Figure 13.13 shows the page displayed when browser_detect.html is
opened in any browser other than Internet Explorer 6.
FIGURE 13.12:
FIGURE 13.13:
NOTE
For a common sense approach to the use of browser detection in your pages,
see Browser Detection and Cross-Browser Support at http://devedge
.netscape.com/viewsource/2002/browser-detection/.
Whats Next?
In this chapter, you learned about Internet Explorer 6, web standards,
and testing your pages for compliance with web standards. Now its
time to look at that other major web browser and learn about Netscape
Navigator 6 and 7.
Copyright 2003 SYBEX Inc., 1151 Marina Village Parkway, Alameda, CA 94501. World rights reserved.