Forum Moderators: open
Any help to explain would be appreciated... :)
html:
<div id="gig0" class="switchcontent">
<h3>heading</h3>
<div class="gigguide">
<p>contents</p>
</div>
</div>
<div id="gig1" class="switchcontent">
<h3>heading</h3>
<div class="gigguide">
<p>contents</p>
</div>
</div>
Included where I snipped to "contents" above is the ability to feel like they are 'paging through' if the user clicks on the link, although mentioning this may be overkill because we don't get that far, here are those links anyway...
<a href="javascript:expandonegigs('gig0')">page 1</a>
<a href="javascript:expandonegigs('gig1')">page 2</a>
related snippets from the javascript:
function getObj(name) {if (document.getElementById) {
this.obj = document.getElementById(name)
this.style = document.getElementById(name).style
} else if (document.all) {
this.obj = document.all[name]
this.style = document.all[name].style
} else if (document.layers) {
this.obj = document.layers[name]
this.style = document.layers[name]
}}
function contractallgigs(){
var inc=0
while (inc < totalgigs) {
var x = new getObj("gig"+inc)
x.style.display="none"
inc++
}
}function expandonegigs(gigid){
contractallgigs()
var x = new getObj(gigid)
x.style.display="block"
}function do_onload(){
expandonegigs('gig0')
}document.write('<style type="text/css">\n')
document.write('.switchcontent{display:none;}\n')
document.write('</style>\n')if (window.addEventListener){
window.addEventListener("load", do_onload, false)
} else if (window.attachEvent) {
window.attachEvent("onload", do_onload)
} else {
window.onload=do_onload
}
Like I say, this appears to work in all browsers tested so far but not Mac IE where I believe the content next on the page replaces it.
Thanks in advance for any pointers as to what I might have done wrong in general, or what I might have done wrong wrt Mac IE in particular.
Dave
To test, try putting:
[blue]alert(window.attachEvent)[/blue] and see what your Mac-equipped viewer sees [ function(){[blah]} or undefined ]
Maybe you could go for the 'safer' option below, although you do have to make sure that, if you have any other onload functions they are kicked off in the same place. No onload handler in <body> for example.
[blue]window.onload = do_load[/blue] or
[blue]window.onload = function()
{
do_load()
+ contents of onload HTML handler (if Exists)
+ function calls from other uses of window.attachEvent('onload',..) (if Exists)
}[/blue]
If Mac IE does not have attachEvent as mentioned as a possiblity, would the line suggested - "window.onload = do_onload" - not have already been performed by the browser as the default (it being in the final else clause of my existing javascript).
I just want to make sure I haven't misunderstood the advice.
It is true to say I have no other onload coding, here or elsewhere.
added: about the <body> onload. Hmm. iiish.
If a browser defaults to the direct window.onload, then that would cancel the <body onload =""> (although, I know, window!= body). The event adding methods don't cancel out other event handlers. Thing is, it should be, by this logic, the functions that were called by the <body> onload that aren't being called. Hmmm.
I have a few things to say about the getObj function. It's mainly about efficiency, so that's not really important...but the script won't work in Netscape 4 (for 2 reasons), and a few other browsers, so it might be better to detect them, and if found, not use the doc.write() to write in the CSS changes (which is a good call, BTW).
Browsers like Netscape 4, Escape, Opera 6-, OmniWeb, Clue browser and WebTV do not allow JavaScript to change the display style.
"[xbrowser] recognises the attachEvent method but does not implement a call"
.. a kind of equally 'phantom method'. If this is true for IE mac, then the first condition evaluates to true, so the default statement is never reached.
Googling: attachEvent IE mac problem
Produces lots of error complaints. Scripts that don't have complaints attached also add an extra (or other) to the first condition:
if(navigator.platform == "Win32" && window.addEventListener)
a) detect Mac and IE, and in that case do the following:
window.onload=do_onload
As suggested and detailed elsewhere too, Mac IE is reported to not like window.attachEvent so that could be the cause.
b) removed the surplus "onload" in my body tag.
One, or both of these together, has resolved the problem identified and thanks for guiding me to it. I daren't remove a) or b) however to find out which fix actually did it! :)