/    Sign up×
Community /Pin to ProfileBookmark

Calling functions

What is the difference between calling a function in HEAD and calling a function in BODY??

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Typhoon101Apr 24.2007 — When an html page loads, it reads your code from top to bottom. Therefore any script written in the head is performed before the actual site content is written to the page.

Although you can write functions in the body, it is good practice to include all of your scripts either in an external file or within the head. It makes things neat.

Within the body you should only really use JavaScript to call functions that were previously written in the head.
Copy linkTweet thisAlerts:
@Kathrine22authorApr 24.2007 — If I want to run the code of a function whan the document is loaded where should I call it?
Copy linkTweet thisAlerts:
@Typhoon101Apr 24.2007 — You would still write it in the head. However, you would need to insert it in an onload function.

For example
[code=html]
window.onload = function(){
your code here
}
[/code]
×

Success!

Help @Kathrine22 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...