/    Sign up×
Community /Pin to ProfileBookmark

my function only works with window.onload – why?

I’m new to javascript and most of my scripts/functions have been cribbed and hacked from other sources. While this generally allows me to put together an appropriate web page, it does leave me lacking in understanding.

I’m working on a menu for my portfolio (student work). This menu is circular in layout based on how many divs are inside a parent div with the id of ‘circle’ and the first function that runs at window.onload is called ‘layout’. Some of these divs are just basic links, while others are images which I would like to use with a lightbox script. The lightbox needs to load at window.onload or body onload, but I can’t seem to load my layout function with any other functions either one after the other (separated by semi-colons) or inside another function like ‘init’ for example. Also, I can only seem to run this ‘layout’ function from window.onload and not just onload inside the body tag.

I’m hoping someone can explain to me why.

Here’s the code for my layout function:

[CODE]
function layout()
{
var circle = document.getElementById(‘circle’)
var circleList = circle.getElementsByTagName(‘DIV’)
layoutCircle(circle,xRadius,yRadius,rotation)
}
[/CODE]

and here is how I am presently calling it (in between SCRIPT tags on the main web page):

[code]
window.onload = layout;
[/code]

‘layout’ is a function contained inside a script file called ‘my_circle_layout.js’ and it is sourced in the header prior to the script block calling window.onload:

[code]
<script src=”js/my_circle_layout.js” type=”text/javascript”></script>
[/code]

Any help would be appreciated.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@lordspaceMay 06.2011 — It's different for every browser.

you could use jquery ready method (below) or read more on (see the comments as well) http://simonwillison.net/2004/May/26/addLoadEvent/

[code=html]<!DOCTYPE html>
<html>
<head>
<style>p { color:red; }</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function () {
$("p").text("The DOM is now loaded and can be manipulated.");
});
</script>

</head>
<body>
<p>Not loaded yet.</p>

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@KorMay 06.2011 —  The lightbox needs to load at window.onload or body onload, but I can't seem to load my layout function with any other functions either one after the other (separated by semi-colons) or inside another function like 'init' for example. Also, I can only seem to run this 'layout' function from window.onload and not just onload inside the body tag.[/QUOTE]
Confusing. But I guess that your problem is that you try to use both window.onload and <body onload=""> which will not work, because it will be a sort of duplicate. I suspect that you need to launch several functions onload. If so, don't write anything in BODY, and group everything inside the javascript code:
<i>
</i>window.onload=function(){
someFunction();
anotherFunction();
// ... other functions, expressions, statements, etc
}
Copy linkTweet thisAlerts:
@KorMay 06.2011 — It's different for every browser.[/QUOTE]
It is not.

you could use jquery ready method (below)
[/quote]

No need to use JQuery for such a simple thing like onload. Do you think JQuery is another language? No. JQuery [I]is [/I]JavaScript structured in a library.
Copy linkTweet thisAlerts:
@lupinauthorMay 06.2011 — Confusing. But I guess that your problem is that you try to use both window.onload and <body onload=""> which will not work, because it will be a sort of duplicate. I suspect that you need to launch several functions onload. If so, don't write anything in BODY, and group everything inside the javascript code:
<i>
</i>window.onload=function(){
someFunction();
anotherFunction();
// ... other functions, expressions, statements, etc
}
[/QUOTE]


I guess I could have been clearer - I attempted to use one or the other with window.onload or body onload. The 'layout' function doesn't seem to work at all with a body onload call, and I can't seem to get it to work from within a grouping function (that's what I meant by using the word 'init' as an example) like in your example. It's mystifying to me. I provided the 'layout' code in case an expert could spot some code that causes this quirk.
Copy linkTweet thisAlerts:
@KorMay 06.2011 — post a link to a test page. I need to see the whole picture.
Copy linkTweet thisAlerts:
@lordspaceMay 06.2011 — It is not.[/QUOTE]

if they are the same for all browsers why do people have these calls ?

if (window.addEventListener){
window.addEventListener('load', loadCB, false);
} else if (window.attachEvent){
window.attachEvent('load', loadCB);
}
Copy linkTweet thisAlerts:
@lupinauthorMay 07.2011 — Confusing. But I guess that your problem is that you try to use both window.onload and <body onload=""> which will not work, because it will be a sort of duplicate. I suspect that you need to launch several functions onload. If so, don't write anything in BODY, and group everything inside the javascript code:
<i>
</i>window.onload=function(){
someFunction();
anotherFunction();
// ... other functions, expressions, statements, etc
}
[/QUOTE]


Kor,

I don't know why - possibly a typo on my part - but I went back to my code and used your window.onload copy and now I can get multiple functions to load, including that 'layout' function which was giving me the problems in the first place. I know I had tried a similar code, so I must have made a mistake or a typo. Thanks again.
Copy linkTweet thisAlerts:
@KorMay 09.2011 — if they are the same for all browsers why do people have these calls ?

if (window.addEventListener){
window.addEventListener('load', loadCB, false);
} else if (window.attachEvent){
window.attachEvent('load', loadCB);
}
[/QUOTE]

There are two ways to create events. DOM 0 straight assignment:
<i>
</i>element.onevent=function(){}

and DOM 1 attaching events models (attachEvent [IE model] and addEventListener [full DOM compatible browsers]) - the code you have posted.

There is a difference. The straight DOM 0 model will create an event, but it will override the same event, if it was previously created. The attaching models will create a new event [I]or will add new functions[/I] to an already existent event.

DOM 0 model is fully crowssbrowser, while there are 2 DOM 1 models.
×

Success!

Help @lupin 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.18,
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,
)...