JavaScript is one of several popular languages that can enable Web pages to interact with users more quickly and efficiently (see Vbscript, php, and scripting languages). The language first appeared in the mid-1990s’ Netscape 2 browser under the name LiveScript. Technically, JavaScript is the Sun Microsystems trademark for its implementation of a standard called ECMAScript. Despite the name, Java¬ Script is not directly related to the Java programming lan-guage.
In its early years JavaScript was perhaps a victim of its own success. Having a relatively easy-to-use scripting lan-guage provided an easier way to add features such as 3D buttons and pop-up windows to formerly humdrum Web forms. However, as with an earlier generation’s fondness for multiple fonts, early JavaScript programmers were often prone to add unnecessary and confusing clutter to Web pages. Besides sometimes annoying users, early JavaScript also suffered from significant differences in how it was implemented by the major browsers. As a result, Netscape users were sometimes stymied by JavaScript written for Microsoft Internet Explorer, and vice versa. Finally, browser flaws have sometimes allowed JavaScript to be used to com-promise security such as by installing malware-infested “browser helpers.” As a result, many security experts began to recommend that users disable JavaScript execution in their browsers.
Using JavaScript
JavaScript syntax and language constructs are similar to those of C, with the addition of basic object-oriented features (see object-oriented programming). The language itself has no capabilities for manipulating the environment (such as input/output). Instead, JavaScript calls upon an “engine” written for each host environment (normally a Web browser). The engine implements features designed to control how a Web page interacts with the user, such as the display of windows and controls such as menus, buttons, or toolbars. JavaScript can also be used to validate a Web form in the browser before it is submitted to the server. In gen-eral, “browser side” JavaScript processing reduces the load on Web servers while allowing pages to respond quickly, such as by changing graphics as the user’s mouse pointer passes over parts of the page.
The principal interface between JavaScript and HTML pages is the Document Object Model (see html and docu-ment object model). A World Wide Web Consortium (W3C) standard defines the DOM functions, and most browsers now consistently support Levels 1 and 2 of these standards. How-ever, there are many Web users who cannot run standard JavaScript, such as users with visual disabilities (see disabled persons and computing), users of some mobile browsers (such as for PDAs or smart phones), or users who have sim-ply disabled JavaScript for security reasons. Therefore, when JavaScript is used for essential page functions (such as form processing), the developer should provide an alternative way for the user to perform the relevant task. (In the case of dis-abled users, this may be a legal requirement.)
Traditionally, JavaScript code has been embedded directly in the containing HTML page, using tags like the following:
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”> <html dir=“ltr” lang=“en”>
<head>
<title>JavaScript Example</title> <body>
<script type=“text/JavaScript”>
var Name = prompt (“Enter your name”,“”);
alert(Name);
</script>
</body>
</html>
When a JavaScript-enabled browser encounters this code, a text box will prompt the user for a name, which is stored in the variable Name and then displayed in an alert box. In modern Web design to XHTML standards, however, just as formatting information is kept in a separate docu-ment (see cascading style sheets) JavaScript code is also maintained in a separate file and simply linked to within the HTML page:
<script type=“text/javascript” src=“mainscript.js”></script>
JavaScript can do much more than just display infor-mation or process forms. JavaScript can access a varietyof Web services (such as databases and search engines) and create custom pages in response to user actions (see also Ajax). JavaScript can also be embedded in applications other than Web browsers: for example, the Adobe Acrobat and Reader and even operating-system scripting (such as Microsoft’s JScript and JScript.NET). Although attention in recent years seems to have shifted more to languages such as PHP, JavaScript remains a widely used and powerful Web design tool.
No comments:
Post a Comment