Russ Ferguson and Christian Heilmann Beginning JavaScript with DOM Scripting and Ajax Second Edition 10.1007/978-1-4302-5093-7_1 Russ Ferguson 2013
1. Getting Started with JavaScript
This book will teach you about JavaScript and how to use it in a practical manner. After you read it, youll be able to
Understand JavaScript syntax and structures.
Create scripts that are easy to understand and maintain.
Write scripts that do not interfere with others.
Write scripts that make web sites easier to use without blocking out non-JavaScript users.
Write scripts that are independent of the browser or user agent trying to understand themwhich means that in some years they will still be usable and wont rely on obsolete technology.
Enhance a web site with JavaScript, and allow developers without any scripting knowledge to change the look and feel.
Enhance a web document with JavaScript, and allow HTML developers to use your functionality by simply adding a CSS class to an element.
Use progressive enhancement to make a web document nicer only when and if the user agent allows for it.
Use Ajax to bridge the gap between the back end and client side, thus creating sites that are easier to maintain and appear much slicker to the user.
Use JavaScript as part of a web methodology that enables you to maintain it independently without interfering with the other development streams.
What you will not find here are
JavaScript applications that are browser specific
JavaScript that is only there to prove that it can be used and does not enhance the visitors experience
JavaScript that promotes unwanted content, such as pop-up windows or other flashy techniques like tickers or animation for animations sake
JavaScript is essential in modern web development, but you cannot take it for granted that the visitor will be able to use or even experience all the effects and functionality you can achieve with JavaScript. You can use JavaScript to completely change the web page by adding and removing or showing and hiding elements. You can offer users richer interfaces such as drag-and-drop applications or multilevel drop-down menus. However, some visitors cannot use a drag-and-drop interface because they can only use a keyboard or rely on voice recognition to use our sites. Other visitors might be dependent on hearing our sites rather than seeing them (via screen readers) and might not be notified of changes achieved via JavaScript. Last but not least, there are users who just cannot have JavaScript enabledfor example, in high-security environments like banks. Therefore, you need to back up a lot of the things you do in JavaScript with solutions on the server side.
Note
Web design has matured over the yearslong ago we stopped using FONT tags, and we deprecated visual attributes like bgcolor and started moving all the formatting and presentational attributes to a CSS file. The same cleaning process has also happened to JavaScript. Now the content, structure, presentation, and behavior of web sites are all separated from each other. Web development now is done for business and for helping the user rather than done for the sake of putting something out there and hoping it works in most environments.
JavaScript is now part of an overall development methodology, which means that you develop it not to interfere with other technologies like HTML or CSS, but to interact with them or complement them.
Web development has come quite a way since the 1990s, and there is not much sense in creating web sites that are static and fixed in size. Any modern web design should allow for growth as needed. It should also be accessible to everyone (which does not mean that everybody gets the same appearancea nice multicolumn layout, for example, might make sense on a high-resolution monitor but is hard to use on a mobile phone or a tablet)and ready for internationalization. You cannot afford any longer to build something and think itll last forever. Because the Web is about content and change, itll become obsolete if we dont upgrade our web products constantly and allow other data sources to feed into it or get information from it.
Enough introductionsyou got this book to learn about JavaScript, so lets start by talking quickly about JavaScripts history and assets before diving right into it.
In this chapter, youll learn
What JavaScript is, and what it can do for you
The advantages and disadvantages of JavaScript
How to add JavaScript to a web document, and what its essential syntax is
Object-oriented programming (OOP) in relation to JavaScript
How to write and run a simple JavaScript program
Chances are that you have already come across JavaScript and already have an idea of what it is and what it can do, so well move quite swiftly through some basics of the language and its capabilities first. If you know JavaScript well already, and you simply want to know more about the newer and more accessible features and concepts, you might skip to Chapter 3 . However, there might be some information youve forgotten, and a bit of a review doesnt hurt, either.
The Why of JavaScript
In the beginning of the Web, there was HTML and the Common Gateway Interface (CGI). HTML defines the parts of a text document and instructs the user agent (usually the web browser) how to show itfor example, text surrounded by the tags becomes a paragraph. Within that paragraph, you might have tags that define the main page heading. Notice that for most opening tags, there is a corresponding closing tag that begins with </</span>.
HTML has one disadvantageit has a fixed state. If you want to change something, or use data the visitor entered, you need to make a round-trip to a server. Using a dynamic technology (such as ColdFusion, Ruby on Rails, ASP.NET, PHP, or JSP), you send the information from forms, or from parameters, to a server, which then performs calculations, tests, database lookups, and other such tasks. The application server associated with these technologies then writes an HTML document to show the results, and the resulting HTML document is returned to the browser for viewing.
The problem with that is it means every time there is a change, the entire process must be repeated (and the page reloaded). This is cumbersome and slow. It is true that at least the Western world has the benefit of fast Internet connections these days, but displaying a page still means a reload, which could be a slow process that frequently fails. (Ever get an Error 404 ?)
Some information, such as the results of performing calculations and verifying the information on a form, might not need to come from the server. JavaScript is executed by the user agent (normally a browser) on the visitors computer. We call this client-side code . This could result in fewer trips to the server and faster-running web sites.
What Is JavaScript?
JavaScript started life as LiveScript , but Netscape changed the namepossibly because of the excitement being generated by Javato JavaScript. The name is confusing, though, because there is no real connection between Java and JavaScriptalthough some of the syntax looks similar.