JavaScript Guide | Part 2

JavaScript Guide | Part 2

JavaScript Language Implementation

There are different implementations of the core JavaScript language, including:


JavaScript Environment

Script Tags

The main tag is the scripting language JavaScript script tag. This tag is an extension of HTML in that it allows the management of external code that is not native HTML.

<script type=”text/javascript”>
/ / <! [CDATA [
JavaScript statements …
/ /]]>
</ Script>

A document may have multiple parts in the definition of the SCRIPT tag. Through this tag can be used and depending on the version of browser you have the proper interpretation of the code. The definitions are as follows:

<script type=”text/javascript”> … </ Script>

Example – Hello, world!

The following example creates a window with a warning written in “Wikipedia Forever.”

<script type=”text/javascript”>
alert (“Wikipedia Forever”);
</ Script>

To “write” directly in the document:

<script type=”text/javascript”>
document.write (“Hello”);
</ Script>

The MIME type for JavaScript source code source code is application / x-javascript, but is most commonly used text / javascript, even if not standard.

To integrate the Javascript code in an HTML document, you must precede it by:

<script type=”text/javascript”>

and follow:

</ Script>

Older browsers typically require that the code starts with:

<script language=”JavaScript” type=”text/javascript”>
<! –

and ends with:

/ / ->
</ Script>

The flags comment <! – … -> Are necessary to ensure that the code is not displayed as text by very old browsers that do not recognize the <script> tag in HTML documents, and LANGUAGE is an HTML attribute (whose use is now deprecated) that may be requested by older browsers. However, <script> documents in XHTML / XML does not work if you commented, because the parser standards compliant XHTML / XML comments are ignored and may also have problems with the symbols – <and> in scripts (eg confuse them with the operators of decrease of integers and comparison). XHTML documents should therefore include the script as XML CDATA sections, by preceding

<script type=”text/javascript”>
//<![ CDATA [

make them follow

</ Script>

(The symbol ‘/ /’ at the beginning of a line indicate the start of a comment JavaScript, to prevent <! [CDATA [and]] are analyzed by the script.)

HTML elements may contain intrinsic events that may be associated with operators specified by a script. To write code valid HTML 4.01, the web server should return a ‘Content-Script-Type’ with value ‘text / javascript’. If your web server can be configured to do so, the author of the website can add the following declaration in the header section of the document

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

JavaScript – Incompatibility

The various implementations of JavaScript, as they did for HTML, often do not conform to standards, but are built to work with a specific web browser. The current ECMAScript standard should theoretically be the basis for all implementations Javascript, but in practice browsers Mozilla (and Netscape) use JavaScript, Microsoft Internet Explorer uses JScript, and other browsers like Opera and Safari using other ECMAScript implementations, often with more non-standard features to allow the compatibility with JavaScript and JScript.

JavaScript and JScript contain many features that are not part of official standard ECMAScript, and can also be free of other features. In this way, are partly incompatible, leading script writers to meet these problems between the two, JavaScript is more compliant, which means that a script prepared in accordance with ECMA standard will work with most browsers except Internet Explorer.

Another effect is that each browser may treat the same script in different ways, and what works in a browser might not work in another browser, or even different versions of that browser. As with HTML, so it is advisable to write standards-compliant code.

The VBScript for Microsoft Internet Explorer is not JavaScript, and is incompatible with the ECMA standard.

Techniques For Fighting Incompatibilities

There are two main techniques to manage the incompatibilities: browser sniffing (lit. “browser sniffing”) and object detection (object detection). When there were only two browsers that support scripting (Netscape and Internet Explorer), browser sniffing was the most common technique. Controlling a number of properties on the client, which returns information on platform, browser and version, it was possible to discern exactly where the code was running browser. Later, techniques for sniffing became more difficult to implement, given that Internet Explorer began to “cover up” his client information, for example by providing information on the browser more and more inaccurate (the reasons for this behavior of Microsoft have long been the subject of dispute). Later still, the browser sniffing became a kind of complicated art form, that is when other browser scripting have entered the market, each with its own information platform, client version.

The object detection is based on checking the existence of an object’s properties.

set_image_source function (imageName, imageURL)
/ / Test to see if the object ‘document’ has a property ‘images’
if (document.images)
/ / Run only if there exists a vector ‘images’
document.images [imageName]. imageURL = src;

A more complex example is based on the use of Boolean tests online:

if (document.body & &

In this case, the term “” normally cause an error in a browser that does not have the property “document.body” but to use the “& &” ensures that “ “is never called if the” document.body “does not exist. This technique is called lazy evaluation (lit. “lazy evaluation”).

Today, a combination of browser sniffing, object detection, and compliance with standards such as ECMAScript and CSS specifications are used in various proportions to try to ensure that a user never encounters a JavaScript error.



Leave your comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.