1 Structure in JS Sat Feb 05, 2011 5:56 pm
Unknown Data
Registered Member
Structure in JS Learn JavaScript - Part 2 |
There are some basic rules, that should be respected, when we create a script. In this lesson, will I try learning you them as easy as possible. Analysis of the structure in JavaScript In our example from the previous tutorial, did we clearly saw the basic build of JS. We are the tacking the "Hello World" example again.
A JavaScript starts with the tag <script> and ends with </script>. According to the W3C rules, should we specify the type designation. It's the same when we're creating a stylesheet, were we use type="text/css". And in connection with JS are we using type="text/javascript". In the past could we also specify the languageversion fx. language="JavaScript1.0", but this isn't allowed anymore by W3C which clearly prescribes the use of type. The document.write does as it says, writing the text inside the bracks ie. "Hello World". Hide the script for older browsers Not every browsers understand JavaScript because they normally are to old. That could fx. be the previous versions of IE to the version 3.0. Depending on how new your browser is, will it be able to understand more and more. So if people there are visiting you site, have a browser there dosen't support JS, will you off course hide it for them, to avoid a mess. And that is actually very easy, the only thing you have to do, is putting the HTML comment sign <!-- and --> around the inner script. It's very rare that someone have a browser that dosen't support JS, so put the comment sign around the script just to be sure. Comments in JavaScript A comment in JS can be indicated by // or like in CSS with /* and */. I got a example of it here.
// Specify JavaScript comment. /* The JavaScript is going to ignore everything below. */ End aforementioned sign. It's alway good to add comments to a script. This will make the understanding easier, especially with larger scripts. External scripts In the "Hello World" example, have we included the script in our HTML document. But it's actually also possible to link to a script - like shown below.
Note: JavaScript is case sensitive, that means that there are a different, if you write with big and small letters. In the next lesson will we learn about the variables. |
Notice : This tutorial is copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators. |