views:

28

answers:

1

I see a very funny behaviour in my page when it comes to IE6 and IE5.5. I have a script (supersleight if you know about it) that puts PNG's back in business when dealing with IE6 and IE5.5. During execution of this, I want to change the background into using the Explorer alpha filter (if Javascript is turned on, use filter, otherwise stick to solid white).

I do this by:

if(document.getElementById('transparency') != null)
          document.getElementById('transparency').style.filter= "alpha(opacity=60)";

...transparency is the id of the object in question.

Putting this at the end of the HTML page (or anywhere after 'transparency' was initiated) results in the script working. Putting it at the very end of the exterior script (deferred) however results in the filter NOT being applied.

However, when I remove the if statement and just tell the browser to use the filter it works (however only a few of the pages has got the 'transparency' id).

I tried to apply the if statement differently by using an alert box and trying both != null and == null and I get nothing.

This made me very curious so I tested this:

var tt = 5;
if(tt == 5)document.getElementById('transparency').style.filter= "alpha(opacity=60)";

Which gave an even stranger result with an error screen saying

tt is undefined

All of this runs perfectly in IE 7 and above...

I realize this is really two different issues but still... Can anyone give me a clue as to what's going on?

+1  A: 

Does this work?

var t = document.getElementById('transparency');
if (t && t.style) t.style.filter="alpha(opacity=60)";

How about this?

try {
  document.getElementById('transparency').style.filter= "alpha(opacity=60)";
} catch (e) { }
no
@no Thanks!!! Worked perfectly but I'm still VERY curious as to WHY and HOW? Would you care to give me a brief explanation? :) Thanks!!
fast-reflexes
@no Yes both worked great.. thanks again! Would really love to hear briefly why though... can't make this out really :(
fast-reflexes
I'd love to explain it, but I really have no idea why they work... or, I should say, I don't know why the code you posted fails. I just rewrote your code in the way I'd normally write it, hoping (and somewhat expecting) that it would work. I'd also be very interested in hearing an explanation if anyone else can give one.
no
@no Very funny business.. my code worked too.. I just had to leave one row empty under my comment... So, luckily, all the above code works and it's correct. Unfortunately and very illogically, there something about the formatting of Javascript that IE5.5 and IE6 are extremely sensitive to... I haven't yet understood what but I'd LOOVE to hear if anyone knows...
fast-reflexes