Client-side JavaScript

Client-side JavaScript

Client-side JavaScript (CSJS) is JavaScript that runs on the client-side. While JavaScript was originally created to run this way, the term was coined because the language is no longer limited to just client-side, since server-side JavaScript (SSJS) is now available.

Contents

Environment

The most common Internet media type attribute for JavaScript source code is text/javascript, which follows HTML 4.01 and HTML 5 specifications and is supported by all major browsers. In 2006, application/javascript was also registered, though Internet Explorer versions 6 through 8 do not recognize scripts with this attribute. When no type attribute is specified in a script tag, the type value is by default "text/javascript" per HTML 5 specification.

To embed JavaScript code in an HTML document, it must be within a <script> element.

<script type="text/javascript">
 
...JavaScript code here...
 
</script>

In the 1990s and early 2000s, HTML comment marks within the <script> tags (<!-- ... -->) was sometimes required to ensure that the code was not rendered as text by old browsers that did not recognize the <script> tag at all. The deprecated HTML attribute language="javaScript" was also required for some such browsers.[1]

JavaScript in XHTML and XML documents should be in XML CDATA sections, as follows

<script type="text/javascript">
//<![CDATA[
...JavaScript code here...
//]]>
</script>

(A double-slash // at the start of a line marks a JavaScript comment, which prevents the <![CDATA[ and ]]> from being parsed by the script.)[2]

It is best practice for various reasons to reference JavaScript in external script files, e.g.:

<script type="text/javascript" src="hello.js"></script>

To write valid HTML 4.01, the web server should return a 'Content-Script-Type' with value 'text/javascript'. If the web server cannot be so configured, the website author can optionally insert the following declaration for the default scripting language in the header section of the document.

<meta http-equiv="Content-Script-Type" content="text/javascript" />

Hello World example

For an explanation of the tradition of programming "Hello World", as well as alternatives to this simplest example, see Hello world program.

This is the easiest method for a Hello world program that involves using popular browsers' support for the javascript URI scheme to execute JavaScript code. Enter the following as an Internet address (usually by pasting into the address box):

javascript:alert('Hello, world!');

DOM binding

User interaction

Most interaction with the user is done by using HTML forms which can be accessed through the HTML DOM. However there are also some very simple means of communicating with the user:

Events

Element nodes may be the source of various events which can cause an action if a JavaScript event handler is registered. These event handler functions are often defined as anonymous functions directly within the element node.

See also DOM Events and XML Events.

Incompatibilities

Most incompatibilities are not JavaScript issues but Document Object Model (DOM) specific. The JavaScript implementations of the most popular web browsers usually adhere to the ECMAScript standard, such that most incompatibilities are part of the DOM implementation. Some incompatibility issues that exist across JavaScript implementations include the handling of certain primitive values like "undefined", and the availability of methods introduced in later versions of ECMAScript, such as the .pop(), .push(), .shift(), and .unshift() methods of arrays.

JavaScript, like HTML, is often not compliant to standards, instead being built to work with specific web browsers. The current ECMAScript standard should be the base for all JavaScript implementations in theory, but in practice the Mozilla family of browsers (Mozilla, Firefox and Netscape Navigator) use JavaScript, Microsoft Internet Explorer uses JScript, and other browsers such as Opera and Safari use other ECMAScript implementations, often with additional nonstandard properties to allow compatibility with JavaScript and JScript.

JavaScript and JScript contain several properties[clarification needed] which are not part of the official ECMAScript standard, and may also miss several properties. As such, they are in points incompatible, which requires script authors to work around these bugs. JavaScript is more standards-compliant than Microsoft's JScript, which means that a script file written according to the ECMA standards is less likely to work on browsers based on Internet Explorer. This also means every browser may treat the same script differently, and what works for one browser may fail in another browser, or even in a different version of the same browser. As with HTML, it is thus advisable to write standards-compliant code.

Combating incompatibilities

There are two primary techniques for handling incompatibilities: browser sniffing and object detection. When there were only two browsers that had scripting capabilities (Netscape Navigator and Microsoft Internet Explorer), browser sniffing was the most popular technique. By testing a number of "client" properties, that returned information on computer platform, browser, and versions, it was possible for a scripter's code to discern exactly which browser the code was being executed in. Later, the techniques for sniffing became more difficult to implement, as Internet Explorer began to "spoof" its client information, that is, to provide browser information that was increasingly inaccurate (the reasons why Microsoft did this are often disputed). Later still, browser sniffing became something of a difficult art form, as other scriptable browsers came onto the market, each with its own platform, client, and version information.

Object detection relies on testing for the existence of a property of an object.

function setImageSource(imageName, imageUrl) {
    // a test to discern if the 'document' object has a property called 'images'
    // which value type-converts to boolean true (as object references do)
    if (document.images) {
        // only executed if there is an 'images' collection
        document.images[imageName].src = imageUrl;
    }
}

A more complex example relies on using joined boolean tests:

if (document.body && document.body.style) {
    // ...
}

In the above, the statement "document.body.style" would ordinarily cause an error in a browser that does not have a "document.body" property, but using the boolean operator "&&" ensures that "document.body.style" is never called if "document.body" doesn't exist. This technique is called minimal evaluation.

Today, a combination of browser sniffing, object detection, and reliance on standards such as the ECMAScript specification and Cascading Style Sheets are all used to varying degrees to try to ensure that a user never sees a JavaScript error message.

Frameworks

See also

References

  1. ^ Dave Raggett; Arnaud Le Hors, Ian Jacobs (24 December 1999). "Scripts". HTML 4.01 Specification. W3C. http://www.w3.org/TR/1999/REC-html401-19991224/interact/scripts.html. Retrieved 12 March 2011. 
  2. ^ "Script and Style elements". XHTML™ 1.0 The Extensible HyperText Markup Language (Second Edition). W3C. 26 January 2000, revised 1 August 2002. http://www.w3.org/TR/2002/REC-xhtml1-20020801/#h-4.8. Retrieved 12 March 2011. 

External links

Resources

Libraries

Tools

Cooperation with


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать реферат

Look at other dictionaries:

  • Client side Scripting — Le Client Side Scripting (script côté client) fait référence généralement à des programmes dans un contexte web qui s exécutent côté client (navigateur web, par opposition aux scripts Server Side s exécutant côté serveur Web. Principe Les scripts …   Wikipédia en Français

  • Client-side scripting — generally refers to the class of computer programs on the web that are executed client side, by the user s web browser, instead of server side (on the web server).[1] This type of computer programming is an important part of the Dynamic HTML… …   Wikipedia

  • Server-side JavaScript — (SSJS) refers to JavaScript that runs on server side. This term was coined because the language is predominantly used on the client side, i.e. client side JavaScript (CSJS).The first implementation of SSJS was Netscape s LiveWire, included in… …   Wikipedia

  • Client Side Scripting — Le Client Side Scripting (script côté client) fait en général référence à des programmes dans un contexte web qui s exécutent côté client (navigateur web, par opposition aux scripts Server Side s exécutant côté serveur Web. Principe Les scripts… …   Wikipédia en Français

  • Comparison of server-side JavaScript solutions — This is a list of Server side JavaScript solutions. Contents 1 Server side JavaScript use 2 See also 3 External links 4 References …   Wikipedia

  • JavaScript — Infobox programming language name = JavaScript paradigm = Multi paradigm: prototype based, functional, imperative, scripting year = 1995 designer = Brendan Eich developer = Netscape Communications Corporation, Mozilla Foundation latest release… …   Wikipedia

  • JavaScript syntax — This article is part of the JavaScript series. JavaScript JavaScript syntax JavaScript topics This box: view · …   Wikipedia

  • JavaScript editor — A JavaScript editor is computer software that allows the editing of program source code for the JavaScript language with features that make it easier to produce correctly functioning objects and functions. Editors for JavaScript typically color… …   Wikipedia

  • JavaScript — Información general Paradigma Multi paradigma, Programación funcional,[1] Programación basada en prototipos, imperativo, Interpretado (Scripting) …   Wikipedia Español

  • JavaScript — Paradigmen: multiparadigmatisch Erscheinungsjahr: 1995 Entwickler: Brendan Eich Aktuelle Version: 1.8  (2008) …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”