A JavaScript consists of JavaScript statements that are placed within the <script>... </script> HTML tags in a web page.
You can place the <script> tag containing your
JavaScript anywhere within you web page but it is preferred way to keep it
within the <head> tags.
The <script> tag alert the browser program to
begin interpreting all the text between these tags as a script. So simple
syntax of your JavaScript will be as follows
<script ...>
JavaScript code
</script>
|
The script tag
takes two important attributes:
·
language: This attribute
specifies what scripting language you are using. Typically, its value will be javascript
·
type: This attribute is what is
now recommended to indicate the scripting language in use and its value should
be set to "text/javascript".
So your
JavaScript segment will look like:
<script language="javascript" type="text/javascript">
JavaScript code
</script>
|
My first java script:
Let us write our class example to print out
"Hello World".
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
document.write("Hello World!")
//-->
</script>
</body>
</html>
|
We added an optional HTML comment that surrounds our
Javascript code. This is to save our code from a browser that does not support
Javascript. The comment ends with a "//-->". Here "//"
signifies a comment in Javascript, so we add that to prevent a browser from
reading the end of the HTML comment in as a piece of Javascript code.
Next, we call a function document.write which
writes a string into our HTML document. This function can be used to write
text, HTML, or both. So above code will display following result:
Hello World!
|