Last few days, I have got several mails from my friends that how to use hidden function in HTML or how to add external javascript file to HTML.
Normally we use java script syntax in the below written manner.
<script language=”javascript”>
This is how we use to insert inside HTML. In this post I will show how to use external javascript to out HTML file.
Let us create a file which will call a function which is defined outside of the HTML.
<html>
<head>
<title> Testing External Button</title>
<script type=”text/javascript” src=”test.js”></script>
</head>
<body>
<form name=”form1″>
<input type=”button” name=”button2″ value=”Click me i am 1″ onClick=’test()’ />
<input type=”button” name=”button1″ value=”Click me” />
</body>
</html>
<head>
<title> Testing External Button</title>
<script type=”text/javascript” src=”test.js”></script>
</head>
<body>
<form name=”form1″>
<input type=”button” name=”button2″ value=”Click me i am 1″ onClick=’test()’ />
<input type=”button” name=”button1″ value=”Click me” />
</body>
</html>
Here this function will call test which is defined outside of the HTML file.
function test()
{
alert(“I am here”);
}
{
alert(“I am here”);
}
This is saved in test.js . I have placed this .js file in the same folder where this HTML file is present.
Done!!!…Now you can call whatever function you want in your flow.
But why I will use External Javascript??
- Hide code from User.
- Reduce load from HTML file
- Easy to use
- Reusability ..You can reuse across different pages.
Image Credit: http://erenbasaran.wordpress.com