07 Dec 2013
Ways of Adding JavaScript code
Following are the ways to add JavaScript in html page.
- Inline : JavaScript code that we writes inside html tags are called inline JavaScript, useful to write short code in JavaScript.
Example:<html>
<body>
<input type=”button” onclick=”alert(‘hello’)” value=”Hello”/>
<input type=”button” value=”Delete” onclick=”return confirm(‘Sure To Delete’)”/>
</body>
</html> -
Internal : JavaScript written in same html page inside script tags are called internal JavaScript.
Example:<html>
<script>
function hello()
{
alert(“hello internal”);
}
</script>
<body>
<input type=”button” onclick=”hello()” value=”Hello”/>
</body>
</html> -
External : JavaScript written in some other ‘.js’ file called external JavaScript. External JavaScript file can be use in more than one file html file.
Example:
Code in ‘myJava.js’ filefunction hello()
{
alert(“hello external”);
}Code in ‘myHtml.html’ file
<html>
<script src=”myJava.js” type=”text/javascript”> </script>
<body>
<input type=”button” onclick=”hello()” value=”Hello”/>
</body>
</html>