Tuesday, November 27, 2007

Where to place my JavaScript code?

JavaScript code can be placed either in between the <head> tag or the <body> tag.
We can also place some scripts in the body tag and some in the head tag if required.

Scripts placed in the head tag.
All the scripts placed in the head tag will be called only when the user triggers an event or when the function in the script is called.

Consider the following example. I had written a javascript function in the head section. This function will be called only when the user clicks on the button(code for buttons and other controls will be written usually in the body tag) on this webpage.

<html>
<head>
<title>example for scripts written in head section</title>
<script type="text/javascript">
function message()
{
alert("You had triggered an event by clicking the button now. so the function message() in the head section is called onclick of the button and i am displayed here, Nice meeting you")
}
</script>
</head>

<body>
<form>
<input type="button" value="click me to check what happens" onclick="message()">

</body>
</html>

Copy the above code to a notepad and save it with .html extension. Now open the saved file from Internet Explorer. You will get an output similiar to the following output. When you get the button click on it to see the alert box. So here the button is written in body and alert box is written in head. When u click on the button the function containing the alert box in the head section will be called.






Scripts placed in the body tag.

All the scripts placed in the body tag will be called as soon as the page loads. For example if you create a html document with the script in the body tag it will be loaded as soon as the html page opens.

Consider the following example.
I had written a JavaScript code in between the body tags. Save the following code in a notepad with .html extension and check out what is happening. As soon you open the page in Internet Explorer (IE), whatever we had written in between the body tag appears. But whereas if we write the same code in head section it will be called only when the event is triggered or the function is called.

<html>
<head>
<title> Examples for scripts written in body section <title>
</head>

<body>

<script type="text/javascript">
document.write("This message is written when the page loads")
<script>

</body>
</html>

Type the above code in a notepad and save it with .html extension. You will get an output similiar to the following one.





External JavaScript files

If we are coding a large piece of code or if the same JavaScript code written in one Html document has to copied to many other HTML documents, in such cases instead of typing it in every HTML document we can create a separate file which has the JavaScript code in one file and can save it with .js extension instead of .htm extension.

Now after creating this .js file we can insert it in all the HTML documents.

Following is an example of how to insert an external .js file to HTML document.

<html>
<head>
</head>
<body>

<script src="myfile.js">
</script>

<p>
This is an example for inserting an external js file to html doc.here myfile.js is the external javascript file.here we had inserted it in the body tag. We can insert in the same way to the head tag also.
</p>

</body>
</html>


For running the above code you have to create a javascript file with name myfile.js and give the path where the file is placed in your computer in the src attribute.
Usually the path of the external file is given in the src attribute.

Monday, November 19, 2007

More about JavaScript

If HTML can create web pages then why do we need JavaScript? The reason is very simple. JavaScript provides interactivity in our pages. HTML can be used to create only static pages. Users can only read the web pages whereas if we use JavaScript users can interact with our pages.

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.


Wednesday, November 14, 2007

Javascript

In this post we will see what JavaScript is and what do we need in order to learn JavaScript.

What is JavaScript?
It is the most popular and widely used scripting language on the internet. It is a friendly language. The reasons why I call it as a friendly language are

  1. It is very easy to learn next to HTML.

  2. You need not compile it like C or Java.

  3. It performs a lot of useful tasks that are more interactive to users like responding to clicks, answering queries, validating data and many more like this. Most of the interactive websites like google maps, gmail etc use JavaScript.



Ok, something said about what is JavaScript we will look into what do we need to start coding JavaScript.


  1. HTML knowledge. If you don’t have any HTML knowledge, please go through my other blog which I had created for HTMLBEGINNERS to understand HTML.


  2. Where will we type the JavaScript code? We need an editor right. Yes, you will need to have a text editor to do coding. If you have windows installed in your machine then NOTEPAD can be used a text editor. Otherwise Editors like Edit Plus can be used. I use Notepad always.


  3. Assume that you had written the JavaScript code on the notepad now how will you run or execute it. You will need to have a web browser that supports JavaScript. All most all the web browsers like Internet Explorer, Netscape, Firefox and Safari support JAVASCRIPT. So you need not worry on this also, if you have a web browser then you can start coding JavaScript now only.

Total Page Hits