473,660 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between empty attribute and undefined attribute

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.getAttribu te('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 '07 #1
4 4668
jp****@gmail.co m 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...t.getAttribute

(scroll down to the Notes)

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

<img />

alt = document.getEle mentsByTagName( 'img')[0].attributes['alt'];
value = alt && alt.specified ? alt.value : 'really_null';
alert(value) // really_null
JW
Apr 24 '07 #2
Daz
On Apr 24, 7:47 am, jpd...@gmail.co m 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.getAttribu te('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.getE lementById(id). getAttribute("a lt"));
}
window.onload = function() {
getAlt("img1");
getAlt("img2");
getAlt("img3");
}
alert("blah");
</script>
</head>
<body>
<img id="img1" src="http://www.google.co.u k/intl/en_uk/images/
logo.gif"><br />
<img id="img2" src="http://www.google.co.u k/intl/en_uk/images/
logo.gif" alt=""><br />
<img id="img3" src="http://www.google.co.u k/intl/en_uk/images/
logo.gif" alt="Google UK Logo">
</body>
</html>
Apr 24 '07 #3
Daz
On Apr 24, 7:47 am, jpd...@gmail.co m 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.getAttribu te('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.

Apr 24 '07 #4
Daz
On Apr 24, 7:47 am, jpd...@gmail.co m 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.getAttribu te('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.

Apr 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
4073
by: Mikko Ohtamaa | last post by:
From XML specification: The representation of an empty element is either a start-tag immediately followed by an end-tag, or an empty-element tag. (This means that <foo></foo> is equal to <foo/>) From XHTML specification:
8
1853
by: tshad | last post by:
I have a function that formats currency to add commas and $. But I also check to see if the field is empty and if it is I return. The problem is that the field now contains the word "undefined". I don't want to have anything in this case. Why is that word showing up and how do I tell it to do nothing? function formatCurrency(num) {
4
15776
by: Manuel Faux | last post by:
Hello! I used the "File Upload - Validator" and the result is like the following: Line 13, column 204: there is no attribute "border" ....irefox!" title="Get Firefox!" border="0" /></a></p> You have used the attribute named above in your document, but the document type you are using does not support that attribute for this
28
2933
by: dingbat | last post by:
I'm writing a "tabbed folder" nav bar. Site standards are graphical prettiness, CSS throughout, valid code, but accesibility is ignored where it conflicts with prettiness. The particular issue here is that the graphic designer wants a pretty (non-webbable) font on the shaded nav tabs, so I'm reduced to using bitmaps. To make the 4-way rollovers ("current" and "hover" states) work I'm using the standard "rollerblind" technique with a...
3
9567
by: Paul T. Rong | last post by:
Do "" and Null both mean nothing?¡¡ If I don't type anything in text box, the its value is Null£¿¡¡Or it is ¡°¡±£¿ I don¡¯ think they are the same, but I don¡¯t know their difference. Thanks.
5
7717
by: sophie_newbie | last post by:
OK this might seem like a retarded question, but what is the difference between a library and a module? If I do: import string am I importing a module or a library? And if i do string.replace() am I using a module or a function or a
3
5147
by: Kceiw | last post by:
Dear all, When I use #include "queue.h", I can't link it. The error message follows: Linking... G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x136): In function `main': G:\Projects\Datastructure\Queue\main.cpp:16: undefined reference to `Queue<char>::Queue()' G:\Projects\Datastructure\Queue\Debug\main.o(.text+0x394): In function `Z10do_commandcR5QueueIcE':
8
2226
by: Cylix | last post by:
Any way to define the length of a textfield? I have just set all the field with a width so that they look like clear and easy to read, However, When I see the page of mac os, They are totally not the result in IE of FF, Does there any way to define it absolutely?
0
1973
by: Thomas Wittek | last post by:
Hi! I'm using xsl:attribute-sets to reduce redundancy in my XSLT. An example from a transformation to XHTML (the attribute values are simply copied from input to output): <xsl:attribute-set name="cellhalign"> <xsl:attribute name="align"> <xsl:value-of select="@align" /> </xsl:attribute>
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.