By | August 2, 2013
For this we can follow three ways, which are given below:
1.      JavaScript in HEAD Element
2.      JavaScript in BODY Element
3.      As an External Script File
    
JavaScript in HEAD Element:  We can use JavaScript in HEAD section by given Method. The script placed inside the HEAD element runs when you perform some action, such as click a link or the submit button.
Code Syntax:
<HEAD>
<SCRIPT type=”text/javascript”>
Script code here
</SCRIPT>
</HEAD>
Example:
<!DOCTYPE HTML>
<HTML>
                <HEAD>
                                <TITLE>JavaScript in HEAD Element</TITLE>
                                <H1>Adding script in the HEAD Element</H1>
                                <SCRIPT type=”text/javascript”>
                                                document.write(“Welcome to the World of JavaScript
<BR/>”);
                                                document.write(“In this example we have used the
JavaScript in the HEAD element.”);
                                </SCRIPT>
                </HEAD>
</HTML>
Output:
JavaScript in BODY Element:  We can use JavaScript in BODY section by given Method. The script placed inside the BODY element runs when a web page start loading ia a web browser.
Code Syntax:
<BODY>
<SCRIPT type=”text/javascript”>
Script code here
</SCRIPT>
</BODY>
Example:
<!DOCTYPE HTML>
<HTML>
                <HEAD>
                                <TITLE>JavaScript in BODY Elementt</TITLE>
                </HEAD>
                <BODY>
                                <H1>Adding JavaScript in Body Element</H1>
                                <SCRIPT type=”text/javascript”>
                                                document.write(“Welcome to the World of JavaScript
<BR/>”);
                                                document.write(“In this example we have used the
JavaScript in the BODY element.”);
                                </SCRIPT>
                </BODY>
</HTML>
Output:

JavaScript as an EXTERNAL Script File:  When JavaScript code created in an HTML file is very lengthy; It affects the readability of the HTML file. In addition, at times we may need to use the same JavaScript code in several Web pages. In such cases, we can store the JavaScript code in an External file and save that file using the ‘.js’ extension. Next we need to link the external file with the HTML document by using ‘src’ attribute of the SCRIPT element to access the script file.
Code Syntax:
<HEAD>
<SCRIPT src=URL of the External .js file>
Script code here
</SCRIPT>
</HEAD>
Example:
<!DOCTYPE HTML>
<HTML>
                <HEAD>
                                <TITLE>JavaScript as an EXTERNAL Script File</TITLE>
                </HEAD>
                <BODY>                              
                                <H2>The sum of First Four Number is:</H2>
                                <H2><script src=”MyScript.js”></H2>
                                </SCRIPT>
                </BODY>
</HTML>
External JavaScript :
var i, count=0;
for(i=0;i<5;i++)
{
                count=count+i;
}
document.write(count);

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *