/    Sign up×
Community /Pin to ProfileBookmark

Call JavaScript function onLoad of the body

I have a function that needs to get the innerHTML between the body tags once the page loads.

However, when I put the JS in the header and call the function using window.onload, it gives me an error. If I put the javascript in the body, it only gets the code above the <script> tag. And I am going to call a server side user control that will put the javascript on the page, so I don’t even know where in the page it will go.

It only works when I use the onload attribute in the body tag. i.e <body onload=”javascript:fcn_name”>

I am using document.body.innerHTML to get the html within the body tags.

I tried using document.body.onload and other ways of doing it. Since I am trying to integrate this function within an already existing site, I cannot go through the hundreds of pages and modify the body tags.

Is there an alternative to using the onload attribute of the body tag?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangApr 10.2010 — In the head:&lt;script type="text/javascript"&gt;
window.onload=function() {
someFunction();
};
&lt;/script&gt;
If this doesn't work then you have other onload events occurring elsewhere.

onload attribute in the body should not be used; it's an outdated method.
×

Success!

Help @aerofanatic 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 5.21,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...