Thursday 25 June 2020

JavaScript Syntax | General form of JavaScript - Online help

JavaScript syntax:

JavaScript Syntax is embedded into an HTML file. A  browser reads HTML files And interprets HTML tags. Since be included as an integral part of an HTML document when required, the browser needs to be informed that specific code is javascript. The browser will then use its built-in JavaScript engine to interpret this code.

JavaScript Syntax | General form of JavaScript - Online help

A JavaScript consists of JavaScript statements that are placed within the <script>................</script> HTML tags in a web page. The script tag takes two important attributes: 

Attributes    Description
Language
  • Indicates the scripting language used for writing the snippet of the scripting code.
  • If left undefined Netscape communicator will assume JavaScript.
  • If left undefined Internet Explorer will assume VB script.
Type 
This attribute is what is now recommended to indicate the scripting language in use and its value should be set to “text/javaScript”.


The syntax of your JavaScript will be as follows:

<script language=”javascript”>
Javascript code
</script>

Or

<script type=”text/javascript”>
Javascript code
</script>

The <script> tag alerts the browser program to begin interpreting all the text between these tags as a script.

Statement is a line of code. This has to be ended with a semicolon(;).

Block is a set of statements put into a pair of brackets. ({,})

Comments:
-Single line comment starts by //.
-Multiline comments start with /* and end with */



Embedding JavaScript in HTML file:

If we are writing small scripts, or only use our scripts in a few pages, then the easiest way is to include the script in the HTML code. 

General syntax of embedding javascript in HTML:

<html>
<head>
<script language=”javascript”>
<! - -
Javascript code
// - ->
</head>
<body>
………….
………..
…………
</body>
</html>

Example:

<html>
<head>
<title>sample program</title>
</head>
<body>
<script type=”text/javascript”>
document.write(“hello readers”);
</script>
</body>
</html>

The above code will display the following result:
hello readers

Explanation of code:

The <script> tag tells the browser to expect a script in between them. You specify the language using the type attribute. The most popular scripting language on the web is JavaScript.

The part that writes the actual text is only one line (document.write(“hello readers”);). This is how you write text to a web page in the JavaScript. This is an example of using a JavaScript function (also known as method).


How to include external JavaScript in HTML file:

If we use a lot of scripts, or our scripts are complex then including the code inside the webpage will make the source file difficult to read and debug. A better idea is to put the JavaScript code in a separate file and include that code in the HTML file. By convention, JavaScript programs are store in a file with the .js extension. The general format is,

<html>
<head>
<script language=”javascript” src=”sample.js”>
</script>
</head>
<body>
………….
………..
…………
</body>
</html>

No comments:

Post a Comment