In this tutorial, You will learn how to add javascript functionality to web pages.

Where do we place the JavaScript in html page?

Including javascript to a html document is very simple. Use the <script>...</script> tag pair. These tags tell the browser to begin interpreting the codes. There are two styles to include javascript to html page.

  1. Inline scripts
  2. External script

Inline Scripts

Write the code between the <script> and </script> elements.
	<script type="text/javascript">
		function SayHello()
		{
			alert("Hello World!");
		}
	</script>	

Your html page source will be like following:

	<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<title>My First Page</title>
			<script type="text/javascript">
				function SayHello()
				{
					alert("Hello World!");
				}
			</script>
		</head>
		<body>
		</body>
	</html>

External Scripts

You can also write in a separate file and link that file to html document. Create a new file and write the following code. Save the file as myscript.js.
	//Content of myscript.js file
	function SayHello()
	{
		alert("Hello World!");
	}

Then, link the javascript file to html doucment. Assign the src attribute to the javascript file path.

	
Your html page source will be like following:
	<html xmlns="http://www.w3.org/1999/xhtml" >
		<head>
		<title>My First Page</title>
			<script type="text/javascript" src="myscript.js"></script>
		</head>
		<body>
		</body>
	</html>



author's photo

Author : Blue Cloud

Just a normal software developer who love to write code. Developing professional software since 1999. After working in the software industry for many years, I've started my own website to share knowledges and experiences.

COMMENTS
blog comments powered by Disqus
Contributors