Professional Documents
Culture Documents
JavaScript
» JavaScript Basics » Introduction to JavaScript - Learn JavaScript
JAVASCRIPT<html><body><script type="text/JavaScript">document.write("This is a
very simple yet powerful JavaScript function");</script></body>
</html>
JAVASCRIPT<html>
<body>
<script type="text/JavaScript">
</script>
</body>
</html>
Now that is a boring example, isn't it? Try with the following and let me know what you think.
Demo
I am a boring paragraph.
Give it a try!
Now let's take a closer look at our first Javascript example and learn some basic concepts. Click
next and let's start.
CSS - Float
JavaScript Basics
References
First thing first. Before you start writing Javascript code, you have to know that you can place the
script in three different locations.
JAVASCRIPT<html>
<head>
<script>
// JavaScript code
</script>
</head>
<body></body>
</html>
JAVASCRIPT<html>
<head></head>
<body>
<script>
// JavaScript code
</script>
</body>
</html>
Inline Javascript
JAVASCRIPT<a href="#" onclick="(function(){ alert('It\'s alive!!');})()">Click
Me</a>
Demo
Click Me
JAVASCRIPT<html>
<head>
<script src="../path/to/file.js"></script>
</head>
<body></body>
</html>
JAVASCRIPT<html>
<head></head>
<body>
.......
<script src="../path/to/file.js"></script>
</body>
</html>
Today this is known as a "not so good practice". There is a generally accepted opinion that you
should separate the HTML markup structure from the processing part. Take a look at this next
example of how we could implement this concept.
<script>
window.addEventListener("DOMContentLoaded", function(event) {
document.getElementById('email').addEventListener("change",
validateEmail);
});
</script>
If you are interested in further reading about this subject, there is a good article located at the
Wikipedia website, here: https://en.wikipedia.org/wiki/Unobtrusive_JavaScript
JAVASCRIPT<script type="text/javascript"></script>
Nowadays, the type="text/javascript" is the default value for the script tag. So nothing happens if
you use it, but you must know that it is not required any more.
JavaScript Basics
References
Miscellaneous