Forum Moderators: open

Message Too Old, No Replies

onLoad function doesn't work in Mozilla

         

lisboa

11:23 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



How is this possible:

I have a function in body like this: <body onLoad="doItAll()"> and this does not work in Mozilla, only in IE!

Then, if I put a link just after the body tag <a href="javascript:doItAll();">do it</a> it works... But I need it to work when the page loads...

Can somewone help me? Or should I be more specific? Does it matter what is defined in doItAll(); function?

Bernard Marx

11:36 pm on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does it matter what is defined in doItAll(); function?

Quite likely. Can we see it?

lisboa

11:50 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Thanks, you are realy fast... Yes, of course you can see:

It is more a sequence of functions. The main goal is to control flash movies and css at the same time and to put it all in a cookie. I have all the code in a js file:

//this function changes styles and is just like I took it

function changeSheets(whichSheet){
whichSheet=whichSheet-1;
if(document.styleSheets){
var c = document.styleSheets.length;
for(var i=0;i<c;i++){
if(i!=whichSheet){
document.styleSheets[i].disabled=true;
}else{
document.styleSheets[i].disabled=false;
}
}
}
}

//The rest I made myself (not easy for me). The next two control css.

function getColor(){
var x = GetCookie('color');
changeSheets(x);
}

function setColor(x){
changeSheets(x);
SetCookie ('color', x, exp);
}

//Now is flash control (3 movies) that change at the same time

function setTitle(newTitle) {
window.document.mytitle.GotoFrame(newTitle);
}

function setMenu(newMenu) {
window.document.mymenu.GotoFrame(newMenu);
}

function setPage(newPage) {
window.document.mymovie.GotoFrame(newPage);
}

function getFlash(){
var y = GetCookie('frame');
setPage(y);
setMenu(y);
setTitle(y);
}

function setFlash(z){
setPage(z);
setMenu(z);
setTitle(z);
SetCookie ('frame', z, exp);
}

function checkIt() {
var fav = GetCookie('color');
if (fav == null) {
setColor(1);
setFlash(2);
}
}

function doItAll(){
checkIt();
getColor();
getFlash();
}

Then, I have cookie code. I don't think it is necessary to put it here..

In the page itself I have this code:

<SCRIPT language=Javascript src="code/cook.js"></SCRIPT>
<link rel="stylesheet" href="code/fractal1.css">
<link rel="stylesheet" href="code/fractal2.css">
<link rel="stylesheet" href="code/fractal3.css">
<link rel="stylesheet" href="code/fractal4.css">
</head>

<body onLoad="doItAll()">
...
</body>

This only works in IE. Why? Is it enough for you to answer or should I put this online? Thank you again...

johnl

8:45 am on Dec 7, 2004 (gmt 0)

10+ Year Member



Though I have not definitely found out in which cases this happens, I also have found that Mozilla's onLoad() doesn't work like one would perhaps expect. Mozilla might start your javascript after the page is loaded but before other adjustments are being made (eg. knowing id's of elements or positioning elements because of css-definitions).
You might try if this is the case here if you insert an alert("pageloaded") as the first instruction in your javascript. This should give enough waiting time to finish said work.
I have no idea how to correct this behaviour though.

Bernard Marx

10:14 am on Dec 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mozilla's onLoad() doesn't work like one would perhaps expect

Wouldn't surprise me!

Mozilla might start your javascript after the page is loaded but before other adjustments are being made (eg. knowing id's of elements or positioning elements because of css-definitions

I would hope this doesn't happen, as it would make the use of an onload handler almost meaningless.

An element is usually referenceable by id immediately after it has been parsed, even, in fact, before the closing tag has been parsed. (Just in case you don't know) Style specifications via stylesheet cannot be read directly from an element via

element.style
.

There are (generally speaking) two ways of assigning event handlers:
1. HTML:

onload = "blah()"

2. Script:
window.onload = function(){blah()}

The anonymous function in #2 could be a function reference instead.
There are newer versions of #2 - I won't go into it.

onload events are usually good for knowing that everything is available in the doc before acting on it. The drawback is that, if there are images to download, the onload won't fire until they have loaded, even though we could have got our script going long before. A scripter's wishlist would include an "oncodeload" event.

I don't know of any difference between the HTML event handler, and the script-assigned version, but it could be along these lines (notice that the handler is on the BODY, but the JS event is a member of window).

Anyway, try this in your script, instead of the

<body onload="doItAll()">

window.onload = doItAll;

-------------------------------------------

The cause of your troubles could simply be referencing:

window.document.mymovie.GotoFrame(newPage);

Does this kind of referencing work in Mozilla for _whatever tag you are using to embed Flash_?

Perhaps...

document.getElementById('myMovie')
// or
document.getElementsByTagName('object')['myMovie']

Myself, I would also take the selective disabling of stylesheets out of the onload function. The stylesheets collection is available immediately, and you should be able to perform this action at any point in the code after the STYLE/LINK tags have appeared. This will prevent any "jumping around" of content.

- So, put the SCRIPT tag (cook.js) beneath the stylesheet links.

lisboa

10:39 am on Dec 9, 2004 (gmt 0)

10+ Year Member



Could't solve it but thanks a lot anyway...