Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View
Essentials of Javascript - Cultural View
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Client-side JavaScript 64<br />
implementations include the handling <strong>of</strong> certain primitive values like "undefined", and the availability <strong>of</strong> methods<br />
introduced in later versions <strong>of</strong> ECMAScript, such as the .pop(), .push(), .shift(), and .unshift() methods <strong>of</strong> arrays.<br />
JavaScript, like HTML, is <strong>of</strong>ten not compliant to standards, instead being built to work with specific web browsers.<br />
The current ECMAScript standard should be the base for all JavaScript implementations in theory, but in practice the<br />
Mozilla family <strong>of</strong> browsers (Mozilla, Firefox and Netscape Navigator) use JavaScript, Micros<strong>of</strong>t Internet Explorer<br />
uses JScript, and other browsers such as Opera and Safari use other ECMAScript implementations, <strong>of</strong>ten with<br />
additional nonstandard properties to allow compatibility with JavaScript and JScript.<br />
JavaScript and JScript contain several properties which are not part <strong>of</strong> the <strong>of</strong>ficial ECMAScript standard, and may<br />
also miss several properties. As such, they are in points incompatible, which requires script authors to work around<br />
these bugs. JavaScript is more standards-compliant than Micros<strong>of</strong>t's JScript, which means that a script file written<br />
according to the ECMA standards is less likely to work on browsers based on Internet Explorer. However, since<br />
there are relatively few points <strong>of</strong> nonconformance, this is very unlikely.<br />
This also means every browser may treat the same script differently, and what works for one browser may fail in<br />
another browser, or even in a different version <strong>of</strong> the same browser. As with HTML, it is thus advisable to write<br />
standards-compliant code.<br />
Combating incompatibilities<br />
There are two primary techniques for handling incompatibilities: browser sniffing and object detection. When there<br />
were only two browsers that had scripting capabilities (Netscape and Internet Explorer), browser sniffing was the<br />
most popular technique. By testing a number <strong>of</strong> "client" properties, that returned information on computer platform,<br />
browser, and versions, it was possible for a scripter's code to discern exactly which browser the code was being<br />
executed in. Later, the techniques for sniffing became more difficult to implement, as Internet Explorer began to<br />
"spo<strong>of</strong>" its client information, that is, to provide browser information that was increasingly inaccurate (the reasons<br />
why Micros<strong>of</strong>t did this are <strong>of</strong>ten disputed). Later still, browser sniffing became something <strong>of</strong> a difficult art form, as<br />
other scriptable browsers came onto the market, each with its own platform, client, and version information.<br />
Object detection relies on testing for the existence <strong>of</strong> a property <strong>of</strong> an object.<br />
function set_image_source ( imageName, imageURL )<br />
{<br />
if ( document.images ) // a test to discern if the 'document'<br />
object has a property called 'images' which value type-converts to<br />
boolean true (as object references do)<br />
{<br />
document.images[imageName].src = imageURL; // only executed if<br />
there is an 'images' collection<br />
}<br />
}<br />
A more complex example relies on using joined boolean tests:<br />
if ( document.body && document.body.style )<br />
In the above, the statement "document.body.style" would ordinarily cause an error in a browser that does not have a<br />
"document.body" property, but using the boolean operator "&&" ensures that "document.body.style" is never called<br />
if "document.body" doesn't exist. This technique is called minimal evaluation.<br />
Today, a combination <strong>of</strong> browser sniffing, object detection, and reliance on standards such as the ECMAScript<br />
specification and Cascading Style Sheets are all used to varying degrees to try to ensure that a user never sees a<br />
JavaScript error message.