With JavaScript, Placement is the Key.

Posted by ActiveEnnovations

The most important thing to know about using JavaScript is where to place it in your webpage. Ultimately this will be based on what your code does and when it needs to trigger. The two places in a website that JavaScript can be placed is the head section and the body section. If you want the scripts to run when the page is loaded place your script code in the “body” section of your website. If you are creating functions that need to be called or triggered by event, this code will go in the head section. It will load when the page loads, but must wait for a function call to activate it. You can use both header scripts and body scripts in your page. There is no limit on the amount of JavaScript your page can contain.

A basic function call would be something as seen below. The JavaScript code is in the head section and does not run until it is called by the onload event of the body section.

<html>

<head>

<script type="text/javascript">

function showAnAlertBox()

{

alert("The alert() is a built in method that displays a message box.");

}

</script>

</head>

<body onload="showAnAlertBox()">

</body>

</html>

You can also save your script code into a text document and refer to it in you webpage. This would be a good way to cut down redundant code and keep the code readable.

<html>
<head>
<script type="text/javascript" src="functions.js"></script>
</head>
<body>
</body>
</html>

Posted on: 6/28/2010 at 11:58 PM
Tags: ,
Categories: JavaScript
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (5) | Post RSSRSS comment feed

Comments

flower

Wednesday, August 25, 2010 7:51 AM

flower

Nice effort, very informative, this will help me to complete my task.I love flowers...I am also interested to send flowers all over the world....

shower kit United States

Thursday, October 21, 2010 3:03 AM

shower kit

wow, this is a really good piece of information. I read about something like this once, this is very excellent material.

Laptop News United States

Saturday, October 23, 2010 4:21 PM

Laptop News

I am building a gaming computer and have decided to shop for the equipment on Newegg.com for the best prices ect. Here is a list of all the things I will hopefully be buying. I know that everything doesnt work with everything so I need somebody to review my the stuff Im buying and make sure that it is compatible

new business ideas United States

Tuesday, October 26, 2010 2:23 PM

new business ideas

Oh man! This blog site is amazing. How can I  make it look like this .

best children's books United Kingdom

Wednesday, October 27, 2010 12:22 AM

best children's books

This really answered my problem, thank you!

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading