A Web developer's guide to the newest cross-browser compatibility issuesSo you think ECMAScript, CSS1, and HTML 4 will solve your cross-browser problems? Wrong!By Shelley Powers
|
has become August 1997 |
Joking aside, while most types of Web content can be created by people with enough time, some aspects of Web technology are very complex and not for the average person. Some applications are best left to the people paid the bucks to take the blame when a technology bursts into flames.
Lately, I've been haunted by a new apparition, thanks to the standards efforts in process and Netscape and Microsoft's promises to follow these standards.
Can people who are not experienced with application development and computer systems create most types of Web content? Definitely, yes! Can they create all of it? Emphatically, no! Can standards aid in creating Web pages that are compatible for browsers that say they adhere to these same standards? Yes. Can they ensure total compatibility? Oh, no, not even.
Adhering to standards is as much a matter of marketing as it is a design philosophy. As an example, Netscape had the de facto standard for scripting Web pages with JavaScript. Microsoft began to challenge Netscape by gaining popularity for its browser, which uses JavaScript in addition to its own version of JavaScript, called JScript, and VBScript. So Netscape takes the approach to get its language defined as the formalized standard as well as the de facto standard.
Netscape could approach ISO as the organization to initiate this process, but ISO is not known for speed. Netscape approached ECMA, a European based standards organization primarily associated with media interface formats. Soon after, Microsoft joined the ECMA membership. Both pronounce their promise to adhere to whatever standard is produced by this organization. ECMA didn't know what hit it.
What is produced from this flurry of big corporate support is ECMA-262, also known as ECMAScript. Released in July 1997, ECMAScript is basically JavaScript 1.1 with the addition of some new or changed data types and support for Unicode.
That's one standard down. However, scripting is not the only issue with Web pages. Page presentation is not an aspect of HTML and because of this people have used some very creative techniques to format their pages. Among some of the tricks used are the 1-pixel blank GIF image and the use of tables for formatting output into columns and controlling the location of elements on a page. The problem with these tricks is that they don't quite work the same with all browsers and in all environments.
It was pretty obvious that some form of standard to control Web page presentation was needed, and the effort for the CSS1 standard was born. This standard provides a style sheet rule specification that determines how an element is displayed, and how multiple style definitions are to combine. It even to some extent provides patterns for user agents (browsers to most of us) to follow in meeting these standards. CSS1 was released as a recommendation in December of 1996. Again, both Microsoft and Netscape promise adherence to the standard, and implemented CSS1 in version 4.0 of their browsers.
Not long after the release of the CSS1 standard, Microsoft and Netscape also collaborated on creating a joint recommendation for CSS1 positioning. Though still a draft, both companies have implemented the static HTML positioning defined in the standard. Additionally, both companies have also exposed the positioning attributes within each of their scripting models, in order to dynamically position or hide elements after a page is displayed.
In July 1997, the W3C issued its draft specification for the eagerly awaited HTML 4.0. This release provides for more complex HTML tables, including the ability to group columns, the ability to group form elements, better support for international and accessibility implementations, and a host of other changes. The W3C sees the HTML 4.0 recommendation as a step towards that day when HTML becomes stable to the point that specification releases will no longer occur, or occur rarely.
HTML covers Web page syntax, scripting covers page processes, and CSS1 covers page presentation. What's missing is basically how this all ties together. To address this, the W3C began work on the Document Object Model (DOM) specification, and requirements for the model are currently at the W3C. Some think that this standard is the one that will literally pull the other standards together and finally ensure cross-platform compatibility. The object model would determine what the key objects are within a Web page, their exposure to dynamic alteration, and what properties, methods, and events they have. In addition, the model will also determine some of the relationships between the objects.
The assumption with all these standards is that you and I can finally write Web pages using the same style sheets, the same scripting language, accessing the same object model, and moving the elements dynamically in the same way and have them perform the same on Microsoft's Internet Explorer and Netscape Navigator. Is this assumption correct? No.
Taking each standard in turn I will demonstrate where incompatibilities exist between Navigator and IE, yet each browser meets the standard. I will also explain how this can happen.
I don't want to get into too much detail on CSS1 as I've covered this in an earlier article (see the resource listing). A brief example of CSS1 follows. This script sets the margins and background of the page and sets the font size and color for the H1 headers. Additionally, the style sheet setting also loads a background image and sets it to repeat along the y-axis:
<styletype="text/css">
BODY{ background-image: url(bar.gif); background-repeat: repeat-y;
background-color: yellow; margin-left: 225px;
margin-right: 0.5in; margin-top: 0.5in }
H1 { font-size: 36pt; color: red }
</STYLE>
This script will produce a Web page with a yellow background, with the header set to 36pt and red, a background image that repeats along the y-axis, and the other margins set to one-half inch. In this case the CSS1 standard has assisted in delivering content that appears relatively the same with both IE and Navigator.
However, what if I add a border to the paragraph element, and also set the white space within the paragraphs to display the same as a preformatted element, meaning that white space is not collapsed:
<styletype="text/css">
BODY{ background-image: url(bar.gif); background-repeat: repeat-y;
background-color: yellow; margin-left: 225px;
margin-right: 0.5in; margin-top: 0.5in }
H1 { font-size: 36pt; color: red }
P{ border-width: 5; border-color: lime;
background-color: white;
border-style:groove; padding: 10;
white-space: pre }
</STYLE>
Figure 1 shows a sample page with the above style sheet setting, viewed from IE 4.0, PP2, and Figure 2 shows the same page viewed from Navigator 4.01. There are noticeable differences in the display of the paragraph. Note, however, that both browsers are in total compliance with the CSS1 specification with their interpretation of the style sheet rules. How can this be when the display is so different?
The differences in the display between the two browsers, which you can check out yourself from the example, has to do with conformance to core or level 1 CSS1, and to their own personal interpretation of information that is not specified in detail in the standard. As an example, the white-spacestyle sheet setting does not have to be implemented by the browser as, according to the core CSS1 standard, this can be ignored in favor of the browser's default handling of white space.
Also, the IE interpretation of the border sets it all the way to the right margin, though the actual paragraph contents end sooner than the margin. Netscape's interpretation sets the border around the contents, not to the margin. Navigator also spaces the content away from the table border while IE places the content right up to the border. Is either browser's interpretation incorrect? Nope. Within the parameters set by the CSS1 standard, the implementation and interpretation of these standards are left up to the user agent unless specifically detailed within the standard.
Both implementation and interpretation of a standard can lead to differences.
This recommendation provides a standard to follow when statically positioning HTML elements (not included in CSS1). Scripting languages build on this foundation and make it possible to conditionally set or dynamically change the visibility, display, and positioning of an element after the page has been displayed. (Thanks to Erik Krock, Netscape technology evangelist, for this clarification).
Both IE and Netscape follow the draft recommendation, exactly, when placing elements statically. As an example, both will position the following block at the exact same position within the Web page:
<DIV style="position: absolute; left:20; top:20; width:300; height: 200; visibility:hidden"> </DIV>
Where the companies differ is when CSS1 positioning is applied dynamically. As stated in an earlier section, standard specifications don't provide for details of implementation. Microsoft and Netscape both dynamically implement this positioning standard, yet each company uses a different approach.
Examining each of their approaches, again using a DIV block, Netscape exposes this element within a page by ownership and then identifier. Based on the fact that the web page document owns the content within the page, this means that you can access a DIV object identified by "test", with the following code:
document.test
First, however, you have to define a style sheet for this object.
To use dynamic positioning, the style sheet for the DIV
object must use absolute positioning, which places an
object at a specific location, rather than within within the natural
flow of the document. You can use a global style sheet setting or a
specific inline style setting to position the element. As
an example, the following uses an inline style sheet setting to
position the DIV block with its upper-left corner 100
units from the top of the Web page document, and 100 units from the
left side of the page:
<DIV id="test" style="position:absolute; left:100; top:100"> Some content </DIV>
Now, to dynamically move the object within a script block, you can set a new value for the left property, the top property, or both:
document.test.left = 50; document.test.top = 50;
In addition to moving the object, you can also clip it, hide it, or turn off its display.
Microsoft supports the same positioning concepts, but uses a slightly different implementation strategy. They have created a style object/property that is attached to each of the HTML elements, and all CSS1 attribute changes are made to this object rather than directly to the element itself. Also, Microsoft exposes the object to coding differently. There are several different ways to access a specific object. You can access it directly by its identifier, or you can access it by its relationship to the all collection, which is a collection of all elements within a Web page. The same code to accomplish the movement of an element called "test" for IE would be:
document.all.test.style.left = 50; document.all.test.style.top = 50;
Or you could use the following:
document.all.items("test").style.left = 50;
document.all.items("test").style.top = 50;
Or even the following:
test.style.left=50; test.style.top=50;
As you can see, regardless of which approach you use with IE, it still won't be compatible with Navigator. Yet, both browsers are complying with the standard as it is currently documented. Except in this case, in addition to differences in implementation, the browsers also differ in their scripting object models.
This isn't to say you can't work around this difference. The following is a simple workaround for the existence of the style property:
//navigator only - use JavaScript1.2 as script block language
function object(obj) {
this.style = obj;
}
theobject = new object(document.theelement);
//ie only - use jscript as script block language
theobject = document.all.theelement;
//both, use javascript as language
theobject.style.left= 50;
theobject.style.top= 50;
The object function can be created in a separate file and linked in. A separate function is used to instantiate theobject. From that point on the new object can be used in elsewhere without having to differentiate the type of browser.
These objects can be extended to cover all details of positioning, including clipping. The details of creating what I call DHTML equalizer objects will be covered in more detail at a later time, but see this code in action with an example from my Web site.
The new HTML 4.0 draft has many very good extensions, not the least of which is increased ease of use for international Web pages, such as which way a Web page reads and which language is being used, as well as issues of accessibility for those who are physically challenged.
There are, however, many areas of confusion within the draft, which definitely opens the door for incompatibilities. And, unlike scripting, incompatible HTML blocks can't be hidden as easily.
As an example, BLOCKQUOTEs are HTML elements that
reference a message or source document. There is absolutely no
specification about how the user agent renders these. As the
specification states, generally the UA will indent the enclosed
text. The specification provides an interesting but rather confusing
comment that style sheets should provide a mechanism to surround the
blocks with quotes, but not to do this by default, and not if the
Web page author already has placed quotes. But, this isn't part of
the actual specification it's only a suggestion. Okay, I'm sure most
UAs won't have problems implementing this consistently.
Actually, very little of the specification determines how elements are actually displayed because HTML can be used in many different environments, from hand-held pocket computers, to speech synthesizers, to 21-inch monitors. I respect this, and understand why this is so. With the addition of style sheets the page author can then apply whatever presentation style they wish based on how they understand the page is being viewed.
However, the HTML specification in essence violates its own
presentation neutrality by providing presentation specifications.
Examples of this are the frames and rules
attribute for the TABLE element. These
newattributes determine how the table is framed and whether
rules will appear between columns, rows, column or row groups, or
everything. Enter conflict, because you can also control borders of
tables, table rows, and table cells with global style sheets. So
what happens if I use a global style sheet setting in combination
with these HTML attributes as shown next:
<HTML>
<HEAD>
<STYLEtype="text/css">
TABLE{ border-width: 5; border-style: groove; border-color: lime }
TD { border-width: 0 }
</STYLE>
</HEAD>
<BODY>
<TABLEframe="vsides" cols="rules">
<TR><TD>cell one</td><td>cell two</td></tr>
<TR><TD>cellthree</td><td>cell
four</td></tr>
</table>
</BODY>
</HTML>
The result of this Web page as displayed in IE 4.0 (which follows the HTML 4.0 draft specification) is shown in Figure 3, and Figure 4 shows the same page but without the style sheet setting. The global style sheet overruled the locally defined table specification or else it overlaid it, a result I wouldn't have expected, and a potential area of confusion for Web page authors.
The DOM is going to be the area that truly defines what a browser can or cannot do, particularly with interactivity in a Web page.
What is an object model? If you consider that any element delimited by a tag within a Web page is an object, then the model specifies what properties of the object can be accessed and altered, what events can be trapped for the object, what built-in methods can be used with the object, and how it relates to all other objects within the page.
As an example, if a header delimited with
<H1></H1> tags is considered an object, the
DOM working group can specify that this header object generate
events such as click or mouseover based on Web
page reader actions. From this, you can trap these events within
script and add behavior to the page based on the combination of
event and object. If the DOM working group specifies that the CSS1
style sheet attributes for a header object are exposed in the model,
you can then change the color of the text, the font-size, or even
remove the header from the page, as a response to some event. When
you consider this, you begin to see how powerful something like the
DOM specification will be.
The lack of a standard object model has been felt in previous
releases of IE and Navigator. As an example, when Netscape released
Navigator 3.0, it exposed images (delimited by <IMG>)
to their scripting object model as the images array. IE 3.0
supported the Netscape JavaScript 1.1 language but not the
Netscape scripting object model. Unfortunately, there was confusion
about the division between scripting language and scripting object
model. Because of this the JavaScript Usenet group received
hundreds of queries as to whether IE truly supported JavaScript 1.1,
comments that the IE implementation was broke, and questions as to
why won't some script that alters the image dynamically work with IE
3.0.
As an example, the HTML 4.0 working draft has listed what's called "intrinsic events" for most HTML elements. These are events such as onmousedown or onmouseover. Yet the question has to be asked, are events a component of HTML or are they components of the DOM? If the DOM lists events other than those listed with the HTML specification does that mean that a browser that supports the events within the DOM standard is no longer in compliance with the HTML standard?
As another example, the HTML 4.0 specification also lists a new
element called the BUTTON element. This element acts as
a container, meaning that you can create a button-like object that
is made up of images, text, headers, or any other HTML content. The
main purpose for this element is to provide a button down appearance
when the web page reader clicks on the element.
However, can't you create this same object using the CSS1
border style sheet attributes? Then, if DOM allows us to
trap events for a class of objects, you can create a button object
that has the inherited ability to display one style of border when
it receives a mouse down event or when the object is first loaded,
and one style of border when it receives a mouse up event.
Encapsulating this object within an external-scripting file you can
distribute it for reuse. Inheriting from this object you can extend
the mouse down event to also trigger another script. The Web page
developer using the object would get all this functionality simply
by including the reference to the scripting file within their web
page, and supplying a class name of "button" to a DIV
block, an image, or any other HTML element.
Not only will this approach prevent the introduction of unnecessary elements to standard HTML, it also provides a means to create reusable components that can be used many times, inherited from, and extended.
For this approach to work, the different standards organizations need to be very careful of overlap and should work together to ensure that each working organization provides specifications for those components of the Web page it should control, and only those components.
Developers are more likely to have compatibility across vendor implementations of an application if one vendor dominates the market and their implementation becomes the de facto standard, rather than being derived from a specification released by a non-profit standards organization. The only exception to this is for standards that are machine-specific, such as the DVD format, and even then the implementation of one vendor probably acted as a seed to the standard. Microsoft has what is the dominant user interface operating system, Windows, with the Mac being a secondary standard. Based on this, you have a plethora of products compatible with one or the other or both. A GUI derived from a standards committee would never have this same impact.
How can you cope with all these standards, and the others I didn't mention such as Extended Markup Language (XML)? Especially in light of the fact that they are no guarantee to browser compatibility?
One approach is to hire someone else, and pay him or her the bucks to take the blame when your Web site breaks. Or, you can freeze your Web pages at the level of technology supported by Navigator 3.0 and IE 3.x. This means that your Web pages will support basically the core functionality of the standards groups, and should be viewable by most browsers, as well as not breaking with future releases of these same browsers. This also means your pages won't be able to take advantage of many of the new features, such as Dynamic HTML, and the enhanced table and form support of HTML 4.0, and so on.
Another approach is that you can use a Web development tool and rely on it to be aware of what the different browsers' support. You won't have as much control over what your pages can do and you may not be able to create some of the more complex page presentations, but you probably will be able to take advantage of some of the newer functionality. Don't forget to test the pages with all your target browsers, though.
You can create the content directly, yourself. With this approach, start small and test at every step. I recommend contructing a Web page using the following 12 steps:
There are approaches to take to create content for both browsers and
I have discussed them in earlier articles, and will continue to
discuss them in the future. Be prepared for frustration at times,
and remember my second rule of Web development: if something doesn't
work, find what does, and make it work for you. My main rule? If you
aren't having fun, take a break, walk away, and then try again.
The Standards
Browser Documentation Pages
Other Resources
LAYERS in Macintosh computers! Has Win95 version too http://www.hexmac.com
Author's Previous Articles on this Technology
About the author
Shelley Powers is an author and has her own Web
development/consulting company, YASD, located in Vermont. YASD's
URLs are http://www.yasd.com
and http://www.dynamicearth.com.
She has co-authored and contributed chapters to books on Java, Powerbuilder 5.0, JavaScript, Perl, and CGI, in addition to writing for NetscapeWorld and other online magazines. She is finishing a book on cross-browser Dynamic HTML for IDG Books Worldwide. Reach Shelley at shelley.powers@netscapeworld.com. HYIP Monitor