Client-side JavaScript (CSJS) is JavaScript that runs on client-side, i.e. the web browser, hence is for client-side scripting. While JavaScript was originally created to run on client-side, this term was coined because the language is no longer limited to just client-side, e.g. server-side JavaScript (SSJS) is also available.
application/javascript, but the unregistered text/javascript is more commonly used.To embed JavaScript code in an HTML document, it must be preceded with:
<script> tag in HTML documents (although script-tags contained within the head-tag will never be rendered, thus the comment markup is not always necessary), and the LANGUAGE attribute is a deprecated HTML attribute which may be required for old browsers. However, <script> tags in XHTML/XML documents will not work if commented out, as conformant XHTML/XML parsers ignore comments and also may encounter problems with --, < and > signs in scripts (for example, the integer decrement operator and the comparison operators). XHTML documents should therefore have scripts included as XML CDATA sections, by preceding them with
// at the start of a line marks a JavaScript comment, which prevents the <![CDATA[ and ]]> from being parsed by the script.)
The easiest way to avoid this problem (and also as a best practice) is to use external script, e.g.:
language is used in the following context:
may contain intrinsic events to which you can associate a script handler.
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.
This is the easiest method for a Hello world program that involves using popular browsers' support for the virtual 'javascript' protocol to execute JavaScript code. Enter the following as an Internet address (usually by pasting into the address box):
See also DOM Events and XML Events.
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 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. However, since there are relatively few points of nonconformance, this is very unlikely.
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.
Object detection relies on testing for the existence of a property of an object.
if (document.images ) // a test to discern if the 'document' object has a property called 'images'
{
document.images[imageName].src = imageURL; // only executed if there is an 'images' array
}
}
A more complex example relies on using joined boolean tests:
if (document.body && document.body.style )
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.