For example, Consider the following scenario if we need a google account, we have to register ourselves first. So here while registering we type our name and enter other details like address, phone number, date of birth etc. Suppose if our phone number is not valid then an error message pops up and our registration fails. We correct the phone number and submit it again. Now again the page validates the information and creates an account if the information typed are correct.
In the above scenario, accepting the user information, validating the details typed and helping in the process of creating an account are all done by JavaScript. A HTML page alone cannot provide this type of dynamic page. So we need JavaScript which we insert inside a HTML document to get this kind of interactivity. JavaScript can even help the programmers to find out the users browsers. We can also create cookies using JavaScript.
Many starters have the confusion that JavaScript is related to JAVA. Or they have the assumption that they should know JAVA to learn JavaScript. No not at all, both JAVA and JavaScript are completely different languages. JavaScript is a lightweight programming language whereas JAVA is a complex programming language.
It is an interpreted language which means that the scripts can be executed without compiling the code.
Ok now where do we write the JavaScript code?
We can either create a separate JavaScript file with .js extension and insert it in our HTML Page or we can directly write the JavaScript functions in the HTML document.
In both the above cases we put the code or the external path in <head> or between the <body> tag of our HTML document.It is always written between the <script> and </script> tag, with the type attribute which tells the browser what scripting language we are using.
We will talk about how to add the javascript code to the html document in detail in my next post. The following is an example.
<html>
<head>
</head>
<br>
<body>
<script type="type/javascript">
document.write("hi! This is my first javascript program!")
</script>
This is my first HTML document.
</body>
</html>
In the above example JavaScript is written between the <script> tags. In the
Beginning script tag only we give the type of the scripting language.
Between the script tags I had used a very common and simple JavaScript function
document.write. It just displays whatever we write inside the document.write function on the web page.
Type the above program on a notepad and save it with .html extension. Open Internet explorer and click on File menu and select “open” option. Search for your saved file and click on open you can find the output as below on the webpage.
No comments:
Post a Comment