收录日期:2020/11/30 16:09:42 时间:2010-09-07 20:21:10 标签:javascript,internet-explorer,internet-explorer-6,if-statement

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?

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) { }