
Starting up our mini-series on Javascript, we’ll be covering some of the basics of Javascript today. Javascript’s syntax was designed to allow non-programmers to add interactively and dynamic content to a webpage, without a prior understanding of a complex programming language. Javascript is able to interact with HTML elements on a webpage. It is capable of reading their content and manipulating them.
Javascript can either be used directly in a web page, or it can be in an external file. Javascript can be added practically anywhere within a document, but should be kept within the head or right before the end of the body. Many people prefer to add Javascript to the end of the document because it won’t slow down the loading of the page as much (since the Javascript won’t be rendered until later in the loading/rendering process).
When using Javascript directly in a document, it needs to be wrapped in special tags. These tags tell the browser to render the contained code as Javascript. We use the following tags to tell the browser this:
[code='xml'][/code]
Furthermore, we also need to add some markup between the script tag and the Javascript code. This will ensure that older browser that do not support Javascript will not output the Javascript code to the browser (it will just ignore it). While there aren’t very many users of browsers that do not support Javascript anymore, we still generally do this (if just for historical/legacy reasons).
[code='xml'][/code]
Yes, they’re almost identical to HTML comments, and that’s the point. Non-Javascript supporting browsers will just see the Javascript code as an HTML comment, and just ignore it. Since these comment tags are in the Javascript specification (which is actually called EMCAScript), modern browsers will ignore the tags, and render the Javascript.
We can also add our Javascript to an external file and load that file into our webpage. This is generally done with large amounts of Javascript, to keep your webpages source cleaner or if you plan on distributing this Javascript. Many people recommend moving Javascript to an external file to “optimize” your webpage, but you should form your own coding styles and opinions on that. Javascript external files use the extension .js and contain just Javascript code. We can load this external file into our webpage like this:
[code='xml'][/code]
Which will tell the browser to load the file external-javscript.js into the webpage. These paths are relative to the current URL.
Next week we will continue our mini-series on Javascript.




