Tweaking your Community

:)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

You are not connected. Please login or register

JS Where To

2 posters

Go down  Message [Page 1 of 1]

1JS Where To Empty JS Where To Thu Apr 22, 2010 2:46 am

iJerick

iJerick
Administrator
Administrator

Where to Put the Javascript

Javascripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, or at a later event, such as when a user clicks a button. When this is the case we put the script inside a function, you will learn about functions in a later chapter.
scripts in scripts in

scripts to be executed when they are called, or when an event is triggered, are placed in functions.

Put your functions in the head section, this way they are all in one place, and they do not interfere with page content.
Code:

<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>

<body onload="message()">
</body>
</html>

scripts in

If you don't want your script to be placed inside a function, or if your script should write page content, it should be placed in the body section.
Code:

<html>
<head>
</head>

<body>
<script type="text/javascript">
document.write("This message is written by Javascript");
</script>
</body>

</html>

scripts in and

You can place an unlimited number of scripts in your document, so you can have scripts in both the body and the head section.

Code:
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert box was called with the onload event");
}
</script>
</head>

<body onload="message()">
<script type="text/javascript">
document.write("This message is written by Javascript");
</script>
</body>

</html>

Using an External Javascript


If you want to run the same Javascript on several pages, without having to write the same script on every page, you can write a Javascript in an external file.

Save the external Javascript file with a .js file extension.

Note: The external script cannot contain the tags!

To use the external script, point to the .js file in the "src" attribute of the tag:


Code:
<html>
<head>
<script type="text/javascript" src="xxx.js"></script>
</head>
<body>
</body>
</html>
Note: Remember to place the script exactly where you normally would write the script!

2JS Where To Empty Re: JS Where To Sun May 02, 2010 7:51 am

JaKerO_024

JaKerO_024
Moderator
Moderator

ahh ok!

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum

 



iJerick | Forumotion | Punbb | jQuery | CSS3
Copyright © 2011 TweakGround™. All Right Reserved.

Free forum hosting | ©phpBB | Free forum support | Report an abuse | Forumotion.com