Thursday 12 July 2012

JavaScript programming goals


JavaScript programming goals

In starting a new endeavor, whether it’s learning JavaScript for the first time or learning better, more modern JavaScript techniques, one ought to have a sense of the goals before starting out. The purpose of a Web site, of course, is for it to be viewable and usable by clients—end users with their Web browsers.

If visitors cannot use a site, you have failed in your job as a Web developer. Toward this end, the site’s functionality should be possible on all browsers, including those on mobile devices, nonvisual browsers, browsers with JavaScript disabled, and simply old browsers. This is easier to accomplish than you might think thanks to an approach called progressive enhancement. Progressive enhancement is the process of creating basic, reliable functionality, and then enhancing that functionality on browsers that support the enhancement. For example, the standard way to handle a form submission is to send the form data to a server-side resource.

JavaScript, as already discussed, can accomplish the same thing using Ajax (as in Figure 1.5). Progressive enhancement says that you should implement the standard approach first, and then intercept that approach when possible. How you implement progressive enhancement will be demonstrated repeatedly throughout this book, starting in the next chapter. This is not to say that there aren’t situations when it’s reasonable to exclude users. For example, it’s not possible for a site demonstrating the wonders of HTML5 to be properly rendered on an antiquated browser. Or, iOS devices—the iPod, iPad, and iPhone—do not support Flash. If a site must absolutely use Flash, it should do so with the understanding that many people will be excluded.

But for the most part, the goal should be to support every browser as much as possible. Not only should a Web site work regardless of the browser, but it should not attempt to break the browser’s normal behavior. For years, JavaScript programmers have attempted to prevent the user from clicking the back button, otherwise using the browser’s history, accessing contextual menus, and so forth. JavaScript, for the most part, should improve the user experience, not radically alter it.

There’s no justification for attempting to make the browser behave in ways other than what the user is accustomed to. (At the very least, if your site relies upon disabling common browser behavior, you’ll eventually run into trouble when a user without JavaScript visits.) Second, to make code easier to maintain, one should also employ the technique of unobtrusive JavaScript. This phrase refers to the separation of JavaScript code from the HTML page, and Chapter 2, JavaScript in Action, starts discussing how this impacts actual code.

Finally, modern JavaScript programming should be appropriate for the current state of the Web as a whole. Think of this like being a model citizen or a good parent: demonstrate the qualities that ought to be emulated. This applies not only to JavaScript, but to HTML and CSS, too. Again, Chapter 2 will establish some parameters toward this end, such as the adoption of semantic HTML. These are the goals of modern JavaScript programming. The goal of this book, then, is to properly implement these goals in real-world code, while simultaneously teaching JavaScript as a language in its own right.

No comments:

Post a Comment