Forum Moderators: open

Message Too Old, No Replies

IE & Mac style amendments using javascript

         

davelms

6:39 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



I've tested the following code using version(s) of IE, Firefox, Mozilla, Opera and Netscape on Windows, and used iCapture to verify it looks ok in Safari. But a user has reported it does not display when using Mac IE (additional info they provided was OS 8.6, IE 6).

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

Bernard Marx

6:58 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't have a Mac to test on, but it may well be that IE Mac doesn't have an attachEvent method.

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]

davelms

7:08 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



Thanks for the suggestion. Can I ask a further question in reply?

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.

davelms

7:16 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



Sorry, it was not true to say I didn't have an onload elsewhere. I did; in "body". It was superfluous, and is now removed. Do you think that is an answer?

Bernard Marx

7:20 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your damn right there!
Sorry. Back to the drawing board.

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.

[howtocreate.co.uk ]

davelms

7:27 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



Don't be sorry, instead I thank you for your support and advice. While it may be off-topic, do let me know your thoughts on efficiency for getObj. Thanks for the link, will go there now...

davelms

8:09 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



I have removed my last edit, I made a mistake!

Thanks for the link - having read that reference I'm still unsure why Mac IE doesn't work but I do now appreciated some of the related comments.

Bernard Marx

8:53 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



aaah..there's something filtering in from the bottom of my mind. It could be a 'phantom memory', but I'll share it with you anyway. Ithink I read something along the lines of:

"[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)

davelms

9:44 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



Thanks, much appreciated and some useful links from google too, cheers. A minor alteration to the solution I think I will take away is to positive check for my failing situation (Mac and IE) and "do something else for Mac and IE". I'm slightly worried the code others have used and is quoted above might exclude something that is currently working for me, so if I change it from a negative to a positive test that should be ok I think... now then, what is "do something else for Mac and IE" :) Much appreciated I should be ok from here unless someone has any further feedback. And once I get details a solution I went for, and confirm it works, I will post back for info. Cheers!

davelms

11:54 am on Jun 27, 2004 (gmt 0)

10+ Year Member



Hi, thanks for the guidance, Mac IE users now report it is working. The two things I did were:

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! :)