Difference between empty attribute and undefined attribute

Discussion in 'Javascript' started by jpdogg@gmail.com, Apr 24, 2007.

  1. Guest

    Hi all,

    I'll trying to tell the difference between the following three cases:

    <img alt="text string" />
    <img alt="" />
    <img />

    I can do this in Firefox with the following code, where elem is the
    HTMLElement representing each image, but IE doesn't seem to
    differentiate between empty string and undefined.

    var alt = elem.getAttribute('alt');
    alt = (alt) ? alt : ((alt===null) ? 'really_null' : "");

    The desired output from running this code on the 3 tags above is:

    text string

    really_null

    It seems like this should be really easy...but I'm having a really
    difficult time trying to figure out what's going on...

    Thanks!
    Jeff
     
    , Apr 24, 2007
    #1
    1. Advertising

  2. schreef:
    > It seems like this should be really easy...but I'm having a really
    > difficult time trying to figure out what's going on...
    >


    This is actually per DOM specifications: if the attribute doesn't exist,
    an empty string should be returned instead of null:

    http://developer.mozilla.org/en/docs/DOM:element.getAttribute

    (scroll down to the Notes)

    For IE, however, you can apply the following workaround:

    <img />

    alt = document.getElementsByTagName('img')[0].attributes['alt'];
    value = alt && alt.specified ? alt.value : 'really_null';
    alert(value) // really_null


    JW
     
    Janwillem Borleffs, Apr 24, 2007
    #2
    1. Advertising

  3. Daz Guest

    On Apr 24, 7:47 am, wrote:
    > Hi all,
    >
    > I'll trying to tell the difference between the following three cases:
    >
    > <img alt="text string" />
    > <img alt="" />
    > <img />
    >
    > I can do this in Firefox with the following code, where elem is the
    > HTMLElement representing each image, but IE doesn't seem to
    > differentiate between empty string and undefined.
    >
    > var alt = elem.getAttribute('alt');
    > alt = (alt) ? alt : ((alt===null) ? 'really_null' : "");
    >
    > The desired output from running this code on the 3 tags above is:
    >
    > text string
    >
    > really_null
    >
    > It seems like this should be really easy...but I'm having a really
    > difficult time trying to figure out what's going on...
    >
    > Thanks!
    > Jeff


    What you want to check for first is whether or not it's the value is
    null. If it's not, then you need to check to see if the value is "".
    If it's not, then all should be great. I think the only time it will
    be set to null, is if you've explicitly set it to null within your
    script. May I ask why you need to know? Generally, you only need to
    get the value, which is either a string, or nothing. If it's nothing,
    then there's not much more you can do unless you set it to something.

    This script should illustrate what you might want to do. Obviously, it
    will require modifying, but I think it demonstrates what is happening.

    <html>
    <head>
    <script type="text/javascript">
    function getAlt(id) {
    alert(id+" =
    "+document.getElementById(id).getAttribute("alt"));
    }
    window.onload = function() {
    getAlt("img1");
    getAlt("img2");
    getAlt("img3");
    }
    alert("blah");
    </script>
    </head>
    <body>
    <img id="img1" src="http://www.google.co.uk/intl/en_uk/images/
    logo.gif"><br />
    <img id="img2" src="http://www.google.co.uk/intl/en_uk/images/
    logo.gif" alt=""><br />
    <img id="img3" src="http://www.google.co.uk/intl/en_uk/images/
    logo.gif" alt="Google UK Logo">
    </body>
    </html>
     
    Daz, Apr 24, 2007
    #3
  4. Daz Guest

    On Apr 24, 7:47 am, wrote:
    > Hi all,
    >
    > I'll trying to tell the difference between the following three cases:
    >
    > <img alt="text string" />
    > <img alt="" />
    > <img />
    >
    > I can do this in Firefox with the following code, where elem is the
    > HTMLElement representing each image, but IE doesn't seem to
    > differentiate between empty string and undefined.
    >
    > var alt = elem.getAttribute('alt');
    > alt = (alt) ? alt : ((alt===null) ? 'really_null' : "");
    >
    > The desired output from running this code on the 3 tags above is:
    >
    > text string
    >
    > really_null
    >
    > It seems like this should be really easy...but I'm having a really
    > difficult time trying to figure out what's going on...
    >
    > Thanks!
    > Jeff


    Whoops. Sorry. I missed the part about there being a problem with IE.
    I'm still working on this for you.
     
    Daz, Apr 24, 2007
    #4
  5. Daz Guest

    On Apr 24, 7:47 am, wrote:
    > Hi all,
    >
    > I'll trying to tell the difference between the following three cases:
    >
    > <img alt="text string" />
    > <img alt="" />
    > <img />
    >
    > I can do this in Firefox with the following code, where elem is the
    > HTMLElement representing each image, but IE doesn't seem to
    > differentiate between empty string and undefined.
    >
    > var alt = elem.getAttribute('alt');
    > alt = (alt) ? alt : ((alt===null) ? 'really_null' : "");
    >
    > The desired output from running this code on the 3 tags above is:
    >
    > text string
    >
    > really_null
    >
    > It seems like this should be really easy...but I'm having a really
    > difficult time trying to figure out what's going on...
    >
    > Thanks!
    > Jeff


    Sorry, it would appear that Microsoft have asked that same question
    that I did. Why do you need to know if it's null or "". Surely you
    only need to know whether the value is an empty string, or a string
    containing text?

    Sorry I couldn't help more.
     
    Daz, Apr 24, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QXNoYQ==?=

    the difference between string.Empty and null

    =?Utf-8?B?QXNoYQ==?=, Nov 2, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    16,782
    Kevin Spencer
    Nov 2, 2004
  2. jakk
    Replies:
    4
    Views:
    12,601
  3. Rohn Farrington

    difference between "" and String.Empty

    Rohn Farrington, Feb 14, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    666
    John Saunders
    Feb 14, 2007
  4. walterbyrd
    Replies:
    7
    Views:
    327
    Asun Friere
    May 17, 2007
  5. Tzury Bar Yochay
    Replies:
    1
    Views:
    420
    Gabriel Genellina
    Mar 24, 2008
Loading...

Share This Page