0% found this document useful (0 votes)
8 views3 pages

04-Where To

Scripts placed in the head section will be executed when called or when an event is triggered. Scripts in the body section will be executed as the page loads. You can place scripts in both the head and body sections. To use the same script across multiple pages, write the script in an external .js file and link to it using the src attribute of the <script> tag.

Uploaded by

BaneeIshaqueK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

04-Where To

Scripts placed in the head section will be executed when called or when an event is triggered. Scripts in the body section will be executed as the page loads. You can place scripts in both the head and body sections. To use the same script across multiple pages, write the script in an external .js file and link to it using the src attribute of the <script> tag.

Uploaded by

BaneeIshaqueK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

JavaScript Where To

JavaScripts in the body section will be executed WHILE the page loads.
JavaScripts in the head section will be executed when CLLE!.
Where to "ut the JavaScript
JavaScripts in a page will be executed i##ediately while the page loads into the browser. This is not
always what we want. So#eti#es we want to execute a script when a page loads$ other ti#es when
a user triggers an event.
Scripts in %head&
Scripts to be executed when they are called$ or when an event is triggered$ go in the head section.
I' you place a script in the head section$ you will ensure that the script is loaded be'ore anyone uses
it.
Exa#ple
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This alert ox !as called !ith the onload event")"
#
</script>
</head>
<ody onload="message()">
</ody>
</html>
Scripts in %body&
Scripts to be executed when the page loads go in the body section.
I' you place a script in the body section$ it generates the content o' a page.
Exa#ple
<html>
<head>
</head>
<ody>
<script type="text/javascript">
document$!rite("This message is !ritten y %ava&cript")"
</script>
</ody>
</html>
Scripts in %head& and %body&
(ou can place an unli#ited nu#ber o' scripts in your docu#ent$ so you can have scripts in both the
body and the head section.
<html>
<head>
<script type="text/javascript">
$$$$
</script>
</head>
<ody>
<script type="text/javascript">
$$$$
</script>
</ody>
)sing an External JavaScript
I' you want to run the sa#e JavaScript on several pages$ without having to write the sa#e script on
every page$ you can write a JavaScript in an external 'ile.
Save the external JavaScript 'ile with a .*s 'ile extension.
Note: The external script cannot contain the %script& tag+
To use the external script$ point to the .*s 'ile in the ,src, attribute o' the %script& tag-
Exa#ple
<html>
<head>
<script type="text/javascript" src="xxx$js"></script>
</head>
<ody>
</ody>
</html>
Note: .e#e#ber to place the script exactly where you nor#ally would write the script+

You might also like