Welcome to WebmasterWorld Guest from 23.20.147.6

Forum Moderators: open

Message Too Old, No Replies

Javascript - differentiating between NULL and empty alt attributes

How can I find/change a script to discriminate between NULL and empty.

     
12:58 pm on Apr 8, 2003 (gmt 0)

New User

10+ Year Member

joined:Oct 4, 2002
posts:14
votes: 0


hi,

How do we discriminate between:

<IMG SRC=".." ALT=""> (empty ALT tag)
and <IMG SRC=".."> NULL alt tag.

Is this possible and how?

Pete

3:47 pm on Apr 8, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 10, 2001
posts:793
votes: 0


I have had a play and a search for you, but as I suspected, without any luck... so as far as I know there is no way of knowing the difference using JS.

According to this article on MSDN [msdn.microsoft.com], I think you should be able to... I haven't managed to get it to work yet though.

- Return Value [of the getAttribute() method]
Variant that returns a String, number, or Boolean value as defined by the attribute. If the attribute is not present, this method returns null.

Maybe if you tell us what you're trying to do with this information we can still help you out?

4:22 pm on Apr 8, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 20, 2002
posts:889
votes: 0


The problem with getAttribute() is that it appears to know that ALT is a valid attribute name for IMG and so returns "" (this behaviour is helpful in most cases). If you invent a new attribute name eg XATT then it returns "" and null depending on whether it is present.

All I can think of is to check the actual HTML like
if (img.att=="" && indexOf(img.outerHTML, "ALT="))

4:50 pm on Apr 8, 2003 (gmt 0)

New User

10+ Year Member

joined:Oct 4, 2002
posts:14
votes: 0


What I would like to achive is to adjust the following javascript favelet to not just show ALT tags when they are provided by also highlight when ALTs are not declared (but not when asigned ="".

javascript:(function(){function toArray (c){var a, k;a=new Array;for (k=0; k<c.length; ++k)a[k]=c[k];return a;}var images, img, altText;images=toArray(document.images);for (var i=0; i<images.length; ++i){img=images[i];altText=document.createTextNode(img.alt);img.parentNode.replaceChild(altText, img)}})();

ideas?

[edited by: tedster at 6:55 pm (utc) on April 8, 2003]

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members