WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You

WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted Structure in JS Sat Feb 05, 2011 5:56 pm

Unknown Data

Unknown Data
Registered Member
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.
Code:
<html>
<head>
<title>Hello World</title>
</head>
<body>
 
<script type="text/javascript">
  document.write("Hello World!")
</script>
 
</body>
</html>

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.
Code:
<html>
<head>
<title>Hello World</title>
</head>
<body>

<script type="text/javascript">
<!-- start hide script

/* Here is a long comment
with multiple lines,
so here can we add a lot of text.
*/
document.write("Hello World!")

// end hide script -->
</script>

</body>
</html>

// 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.
Code:
<script type="text/javascript" src="file.js"></script>
These tags will find the file.js and will then "put it inside your document".

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.

http://woops.dk

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum