/    Sign up×
Community /Pin to ProfileBookmark

I thought onload ran when the page was loaded, not before…

I am having trouble with my onload code running seemingly before the page has loaded. I thought onload was supposed to run once the page had finished loading?? For example,

[CODE]window.onload = countAnchors();

function countAnchors() {

var anchors = document.getElementsByTagName(‘a’);
var numA = anchors.length;
alert(numA);

}[/CODE]

I get 0 when I refresh the page, but 42 when I click a link which runs countAnchors(), as there are actually 42 <a> elements on the page.

Maybe I am wrong in expecting it to wait for the page… I am new to this. If this is working correctly, can you tell me how I can run some code AFTER the page has loaded?

Thanks for any help.

Grape

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@TeufelMay 15.2008 — I can't tell you why you get that error.

At first, I thought that window.onload is called before the DOM content will be loaded, but document.body.onload = countAnchors() has the same effect.

Using <body onload="countAnchors()"> worked fine for me, but I'd too like to know why only this works.
Copy linkTweet thisAlerts:
@mrhooMay 15.2008 — [B]window.onload = countAnchors();



Reference [/B]
the function, don't [B]call [/B]it here.

use window.onload = countAnchors;

or

window.onload =function(){

alert(document.getElementsByTagName('a').length+' links');


}



Calling a function with parentheses following it[B]() [/B]calls the function[B] immediately.

[/B]
Copy linkTweet thisAlerts:
@ProGrapeauthorMay 16.2008 — [B]window.onload = countAnchors();



Reference [/B]
the function, don't [B]call [/B]it here.

use window.onload = countAnchors;

or

window.onload =function(){

alert(document.getElementsByTagName('a').length+' links');


}



Calling a function with parentheses following it[B]() [/B]calls the function[B] immediately.

[/B]
[/QUOTE]


aaaaahhhh thank you very much ?

an easy thing to overlook, at first.

so how would you call a function with parameters? is the only way to do it to write an anonymous wrapper function, like
[CODE]window.onload = function() {
paramFunc(1, true 'param');
}[/CODE]
or is there a special notation to reference functions while also giving them parameters?

thanks again!
Copy linkTweet thisAlerts:
@TeufelMay 16.2008 — //you can use the arguments array within a function:

function foobar() { alert(arguments.length) }

foobar("mouse", 1, location, ["xx", "xxx"]);
×

Success!

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

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

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