The Advantages and Disadvantages of JavaScript (Cont.)
April 26, 2002
Browser Programmability
While the language syntax and features of different versions of JavaScript
are important, even more so is the browser being used to view your web pages.
Let's go back to the basics to explain what this means in terms of JavaScript
development.
JavaScript isn't able to do much without an environment, and in
this book we're working with JavaScript in the browser. In this sense we could
say that the browser is essentially the host.
JavaScript's usefulness lies in its ability to manipulate the host environment,
whether that's a web page, a PDF
file, a web server, or Windows itself.
Each of these host environments makes itself available for programming. It
does this by providing objects
that allow JavaScript to learn about, and manipulate, the host environment.
For example, the browser makes the document object that represents
a web page available to JavaScript. This enables us to use JavaScript to add
new HTML into a web page even as the user is viewing it. If we were using JavaScript
in a Windows server then we'd find that the server exposes a very different
set of objects with functionality related to the server.
So browsers themselves also have different levels of programmability: the collection
of browser elements and features that can be accessed through a programming
language. Just as the HTML supported by different browser versions varies, so
too do the things we can access in the browser. A simple example would be the
ability to change
the image loaded in an IMG tag; this was supported by Netscape
3 but not by Internet Explorer 3, so attempting to access an IMG
tag and change its source image will work great in one browser but fail completely
in the other.
Not only do different browsers enable us to do different things in JavaScript,
the way certain pieces of functionality are implemented can vary between browsers
as well. For example, if we're creating pages accessible to IE4+ and NN4+ browsers,
then we need to cope with three different ways of accessing HTML tags through
JavaScript:
·
The IE4+ way that works exclusively with IE4+
·
The NN4 way that works exclusively with NN4
·
The DOM Interface that works with IE5+ and NN6
browsers
We'll be covering each of these ways in depth in this book.
Coping with Different Browsers
You might be wondering how you can deal effectively with the panoply of JavaScript
versions and web browsers' programmability. Well there's no denying that it
can be frustrating, but there are strategies that you can adopt for dealing
with browser issues that will make life that bit easier.
One option, and the one I prefer if possible, is to do two versions of a web
site. The first version features maximum functionality and works on IE4+ and
NN6. (As you'll see in later chapters, pages can be made to work with both IE4+
and NN6 with a little modification. Making IE4+ pages work with NN4 is much
more of a challenge because of the limited programmability of NN4 compared with
IE4, and the different ways that these browsers allow programmatic access.)
The second version is a more basic site, one that uses only simple HTML and
limited JavaScript, if any at all, but which still allows access to most of
the site's content. As long as I pay careful attention to the design of this
second version of the site then I can expect it to work with 99% of browsers
currently in use out there. Then when a visitor arrives at one of the pages
in the web site I can use browser detection scripts, (which we'll be using later
on), to redirect them to the version of our web site that works with their browser.
If you use this approach to
web development then you should ensure that the information for both versions
comes from the same source; for example, a server-side database could be used
to generate the HTML for both versions.
There will be times when this approach isn't going to work though. Sometimes
the only way to make a web site function as well in NN4 as it does with IE4+
is to dumb down the original site specification. You'll also have to decide,
on a site-by-site, audience-by-audience basis, whether you want to support older
or less common modern browsers like Opera.
Three questions should help clarify which browsers your site needs to work
with:
1.
Who will visit the web site? If you're creating
a web site dedicated to programming the Opera browser then it's going to have
to work with Opera. Similarly, if you're developing for an Intranet where the
browser and version is controlled then the question is answered for you.
2.
What does the client want? If they demand it
works with IE2.0 and remain oblivious to your raised eyebrows and sotto voce
"you have got to be kidding", then that's what you're going to have
to do.
3.
Which browsers are most commonly used? This changes as new browsers
are released and old ones go out of use. You also need to take into account
the importance of making your web site accessible – while this is obviously a
good idea, there are also legal obligations to make certain web sites accessible.
There are a number of web sites that offer general statistics about the browser
versions in common use, and these can help you decide which browser you need
to support in order for the majority of people to view the site. Try these for
web browser stats:
http://www.upsdell.com/BrowserNews/stat.htm
http://browserwatch.internet.com/stats/stats.html
http://www.w3schools.com/browsers/browsers_stats.asp
This last site covers information such as operating system, screen size and
the version of JavaScript being used – including the fact that as many as 1 in 10 people have JavaScript
switched off. (If you were building for such an audience then you might opt
to have a non-JavaScript version of the web site, or create individual non-JavaScript
pages, or notify users that they need JavaScript to use the web site. We'll
be looking at ways of dealing with this as well as dealing with different screen
sizes.)
Remember, though, that many browser statistic web sites base their numbers
on the people who visit their web site, and the people who visit such sites
are unlikely to be representative of the average visitor to your site.
In fact, if you're creating web pages for an existing web site, then that web
site is probably the best source of browser statistics.
However you find your statistics,
always be aware that you should only alienate users for a good reason. Although
you might be only losing 5% of your potential visitors statistically, if those
5% are an extremely vocal minority they can have a detrimental impact on your
brand and good name. A shop that turned away 5% of people because they were
wearing the wrong shoes wouldn't be able to expect much long–term success…
The Advantages and Disadvantages of JavaScript
Practical JavaScript for the Usable Web
JavaScript in a Web Page
|