Closed Thread Icon

Topic awaiting preservation: Testing if style is defined (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8179" title="Pages that link to Topic awaiting preservation: Testing if style is defined (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Testing if style is defined <span class="small">(Page 1 of 1)</span>\

 
viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 05-23-2002 05:47

I have this code:
bf.style.display='block';
But, in a certain situation, "bf" can be "undefined".
How do I test it (if bf is okay), prior to executing it, so if it is undefined I will not call the instruction?
Thanks.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-23-2002 06:06

Try

if (bf) bf.style.display='block';

If that doesn't work, try

if (window.bf) bf.style.display='block';

viol
Maniac (V) Inmate

From: Charles River
Insane since: May 2002

posted posted 05-23-2002 14:05

After asking the question, I found this article (searching for the words "javascript style undefined" in Google): http://www.fusionauthority.com/Article.cfm?ArticleID=2193
It not only solved the problem with "bf" but also my old problem about closing an undefined window (posted here also, sometime ago).
This "typeof" function is really magic.
I used it like this:
if (typeof(bf) != "undefined") { bla bla bla ... }

Also, I needed a way to test the existence of the stylesheet, prior to using some definitions inside that stylesheet (bf !), so I found this function (modified by me to make it work as I wanted):

function getActiveStyleSheet() {
var i, a;
for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 )
return a.getAttribute("title");
}
return null;
}

When I want to test the existence of the stylesheet, I use:
if (getActiveStyleSheet() != null) { bla bla bla... }

It works fine. I'm not sure though if there would be a safer way to do that.



« BackwardsOnwards »

Show Forum Drop Down Menu