• Have Any Question?
  • contact@eitworld.com

Html scripts





    • You can use JavaScript with html by using <script> tag.
    • Javascript is a client side scripting langugae which is used for image manipulation, form validation, and dynamic changes of content.
    • Here is an example of insertion of javascript into html using <script> tag:
<!DOCTYPE html >
<html>
<body>
<h1 id=”hjs”></h1>
<script>
document.getElementById(“hjs”).innerHTML = “This is JavaScript!!”;
</script>
</body>
</html>

To see live output : Visit Try Yourself Editor

Output

    • Note: We will discuss about the JavaScript in detail, in our upcoming JavaScript Tutorial!

 

  • There are two ways to insert JavaScript in html:
  • By creating separate JavaScript file. (External)
  • Directly write Javascript code into html file. (Internal)

By creating separate JavaScript file (External)

 

    • If you want to use same Javascript code into various html documents then it is better to put the JavaScript code into a separate file.
    • A Javascript file will have extension as .js and it will be included in HTML files using <script> tag.
    • Assume that there is a small Javascript file myscript.js as follows:
function js()
{
alert(“This is Javascript”);
}
    • Now let’s include the above external Javascript file myscript.js into our following HTML document:
<!DOCTYPE html >
<html>
<head>
<script src=”http://www.eitworld.com/assets/images/myscript.js” type=”text/javascript”/></script>
</head>
<body>
<input type=”button” onclick=”js();” name=”ok” value=”Click Me” />
</body>
</html>

To see live output : Visit Try Yourself Editor

    • When we execute the above code it will produce the following result:

    • Now when you will click on this button it will produce the following result:

Directly write Javascript code into html file (Internal)

 

    • You can directly write your Javascript code into html file.
    • <script> tag is used for this purpose.
    • Here is the example of internal javascript insertion:
<!DOCTYPE html >
<html>
<head>
<script type=”text/javascript”>
function js(){
alert(“This is JavaScript”);
}
</script>
</head>
<body>
<input type=”button” onclick=”js();” name=”ok” value=”Click Me” />
</body>
</html>

To see live output : Visit Try Yourself Editor

    • When we execute the above code it will produce the following result:

    • Now when you will click on this button it will produce the following result:



Real Time Web Analytics