PDA

View Full Version : Problem with body and window onload event



gazza1234
03-17-2011, 01:18 PM
Can anyone help with a proble I havem on my website

I have the following two scripts on the same page which I downloaded from this site

Chained Menu (http://www.dynamicdrive.com/dynamicindex1/chainedmenu/index.htm) - which requires a <Body> onload event

Daily Iframe Content (http://www.dynamicdrive.com/dynamicindex17/dowiframe2.htm) - which requires a window.onload event

I added both onload events into the <Body> tag

onload="initListGroup('chainedmenu', document.clock.firstlevel, document.clock.secondlevel);clock();createhintbox();dayofweek_iframe() " (red is window onload - black is body onload)

The problem I am having is that in Internet Explorer both the Body and window events load, the chained menu the iframe content displays.

However if I open the page in Firefox only the former works (i.e. Iframe content does not display).

The Iframe content script loads a different page depending on the day (hence the need for the window onlad event); I used this so i can display the sponsors in varying orders. I've temporarily overcome the problem by running an IFrame script which only runs one file so doesn't use the window onload event; but would like to go back if possible.

Is there any way that I can run a Body and Window onload event which will display with firefox

jscheuer1
03-17-2011, 02:42 PM
That's a way to do it. Looks like there might be other scripts involved as well. Set up a page that shows the problem and give us a link to it.

gazza1234
03-17-2011, 09:26 PM
The page is

http://www.stroudskittles.co.uk/signingon-old.html

The Body Tag is

<body bgcolor="silver" text="black" link="blue" vlink="green" alink="red" onload="initListGroup('chainedmenu', document.clock.firstlevel, document.clock.secondlevel);clock();createhintbox();dayofweek_iframe()">

The initListGroup is for the chaned menu (which displays when it's the first onload), clock displays the Date and Time in the form, createhintbox displays thehint (?) at te end.

The problem is the dayofweek_iframe, which is a window.onload event in the scroller which displays the sponsors (removed from <script language="JavaScript1.2" src="iframe-noonload.js"></script>). It should change depending on the day of the week.

Under Firefox this will not display; under IE it works fine

This is a window.onload event the others are Body onload events.

Any help very much appreciated

jscheuer1
03-17-2011, 10:36 PM
It appears to be the hintbox.js script. At the end it has:


if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)
else if (document.getElementById)

You see that highlighted line? It's an if with nothing to do if it is true. Firefox sees this as invalidating the entire hintbox script, so the call in the body onload event to createhintbox() throws an error. As a result, dayofweek_iframe(), which follows that never gets fired.

Here's what I would do for now. get rid of that line so that you have:


if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)

And get rid of createhintbox(); from the body onload event as well. It's not needed because all modern browsers have either addEventListener or attachEvent.

I say for now, because you might want to make all of these events into a single function and use the add/attach methods to fire that one function on window load. Or make each individual script do its window on load event that way. If you do it like that, each individual script could be added or removed without disturbing the others. But let's leave that for another time.

In fact, more and more of the scripts being written today use some form of the add/attach methods for any load event they need. Many though do not need it. They either work completely differently (don't need the page to load first), or use the DOM ready event, which is faster.

gazza1234
03-18-2011, 07:55 AM
Thanks for you help i'll give it a try and let you know how it works

gazza1234
03-18-2011, 12:33 PM
I have followed your suggestion; whilst is has resolved the issue regarding running both the Chained Menu and Sponsor Daily IFrace Content, it has now caused a problem in that the hintbox will not display

On Internet Explorer I receive the following message

Javascript Error

Description:Object expected
Source: http://www.stroudskittles.co.uk/signingon-old.html
Line:105

On Firefox I point to the ? but noting happens

My best option here could be to remove the Hint Boxes, unless anyone has any other suggestions

jscheuer1
03-18-2011, 01:28 PM
The hintbox-test.js appears to be an empty file. I only told you to remove that one line, not the entire script.

If I add back the script, as is, from:

http://www.dynamicdrive.com/dynamicindex16/showhint.htm

It works fine in a local mock up. As long as you don't change anything else, I can put up a demo if you like.

gazza1234
03-18-2011, 03:13 PM
I see what I've done I've save it as a different name (I put suffix -test.js)

Will upload and try again

Thanks

gazza1234
03-18-2011, 03:14 PM
Sorry I put suffix -new (to allow my existing files to remain unchanged).

Problems with trying to make changes in a hurry

gazza1234
03-18-2011, 03:40 PM
Sorted, would help if I enter the crrect file name

Thanks for your help

gazza1234
03-18-2011, 07:16 PM
Hi

Just notice all works fine in IE in Firefox (or at least my version on the PC at home 3.6.6), the Hintbox does not display)

Not a major issue, the drop down is far more important just wondering if anyone has nay ideas why there is the problem in Firefoox

jscheuer1
03-19-2011, 04:40 AM
If you mean on signingon.html, it's like I said before - In its hintbox.js file at the end:




if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)
else if (document.getElementById)


Get rid of that line so that you have:


if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)

gazza1234
03-21-2011, 07:38 AM
Thanks for your help, very much appreciated; all works fine now.

It appears that I edited the Javascript but saved it without the .js extension, therefore the original file was still being used.