Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Different Type of String Value?

1 view
Skip to first unread message

dhtml

unread,
Jun 2, 2008, 1:48:32 PM6/2/08
to
There is a strange issue with IE (IE7 here) and the string value
returned from properties that have not been set. The problem is that
there is an Error thrown when calling getElementById or
getElementsByTagName.

<a id=a>aa</a>

a.rel === ""
=> true

but:
document.getElementById(a.rel);
=> Error: Invalid Argument

What is going on here?

You can try on any web page that has a link. This one will work.

http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html
javascript:alert(document.links[0].rel === "");
javascript:alert(document.getElementById(document.links[0].rel));
javascript:alert(document.getElementById(''));

It appears that there is some sort of internal string representation
for domstring. The comparison / identity operator and typeof operator
appears to do comparison with this domstring, but the value is not
passed internally, and the error propagates out to the browser.

Calling toString, the same [[value]] gets returned (from ToObject):-
javascript:alert(document.getElementById(document.links[0].rel.toString()))

- causing an error. The Object constructor also calls ToObject, but
oddly,
the error does not occur here; the [[value]] of the newly created
String is different:-
javascript:void( alert(new Object(document.links[0].rel).valueOf() ===
''));

javascript:void(function() {alert(document.getElementById(new
Object(document.links[0].rel).toString())) }() )

- the [[value]] is different; it's the real empty string, not the one
the causes errors.

No problems for properties that are not the empty string:-
javascript:alert(document.getElementById(document.getElementsByTagName("a")
[4].id).id)

Empty string is OK, too, so long as its set:
javascript:alert(document.links[0].rel === ""); // Error: Invalid
Argument
javascript:void(document.links[0].rel = "");
javascript:alert(document.links[0].rel === "");


Creating textNode with the domstring does not cause error:
javascript:try{alert(document.createTextNode(document.links[1].rel))}catch(e)
{alert(e.message);}

Can one of the IE Team or JScript team please explain exactly what is
happening?

Garrett

dhtml

unread,
Jun 2, 2008, 3:10:54 PM6/2/08
to
On Jun 2, 10:48 am, dhtml <dhtmlkitc...@gmail.com> wrote:

> Empty string is OK, too, so long as its set:

<correction>
javascript:alert( document.getElementById( document.links[0].rel ) );
javascript: void( document.links[0].rel = "" );
javascript:alert( document.getElementById( document.links[0].rel ) );
</correction>

(the examples should be for getElementById, as above)

>
> Garrett

dhtml

unread,
Jun 2, 2008, 3:10:51 PM6/2/08
to
On Jun 2, 10:48 am, dhtml <dhtmlkitc...@gmail.com> wrote:

> Empty string is OK, too, so long as its set:

dhtml

unread,
Jun 2, 2008, 3:10:56 PM6/2/08
to
On Jun 2, 10:48 am, dhtml <dhtmlkitc...@gmail.com> wrote:

> Empty string is OK, too, so long as its set:

dhtml

unread,
Jun 2, 2008, 3:12:48 PM6/2/08
to
On Jun 2, 10:48 am, dhtml <dhtmlkitc...@gmail.com> wrote:

> Empty string is OK, too, so long as its set:

<correction>


javascript:alert( document.getElementById( document.links[0].rel ) );

javascript:void( document.links[0].rel = "" );

dhtml

unread,
Jun 2, 2008, 3:12:50 PM6/2/08
to
On Jun 2, 10:48 am, dhtml <dhtmlkitc...@gmail.com> wrote:

> Empty string is OK, too, so long as its set:

<correction>

Dave Anderson

unread,
Jun 2, 2008, 3:57:35 PM6/2/08
to
dhtml wrote:
> There is a strange issue with IE (IE7 here) and the string value
> returned from properties that have not been set. The problem is that
> there is an Error thrown when calling getElementById or
> getElementsByTagName.
>
> <a id=a>aa</a>
>
> a.rel === ""
> => true
>
> but:
> document.getElementById(a.rel);
> => Error: Invalid Argument
>
> What is going on here?

Good question. For what it's worth, either of these amount to workarounds:

document.getElementById(a.rel+"")...
document.getElementById(a.rel+="")...

The second has the added benefit of being useful later as an actual empty
string.

--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms.


Anthony Jones

unread,
Jun 3, 2008, 4:55:18 PM6/3/08
to

"dhtml" <dhtmlk...@gmail.com> wrote in message
news:ec1e3830-0a1e-45bd...@x35g2000hsb.googlegroups.com...

To understand what is happening you need to understand how strings work in
COM (the component model on which IEs implementation of the DOM is based).

In COM strings are represented using a structure known as a BSTR. A BSTR is
pointer to the first character of the string. The 4 bytes of memory that
preceed that pointer will contain the number of characters held in the
string. A BSTR is always terminate by a null character.

An empty string "" as a BSTR would need 6 bytes (note unicode is always
used), a pointer to a single null character (2 bytes) and preceeding that
would be 4 byte all zeros ( 00 00 00 00 00 00 ).

Doing something like this would create such a BSTR:-

s = ""


However a BSTR can contain a null pointer (that is the pointer itself is
zero). This can mean un-assigned but in most languages it is treated as
equivalent to an empt. This is true in VB, VBA, VBScript and JScript.

Now to your specific case. The rel property of an anchor returns a BSTR
also the getElementById accepts a BSTR. When an element doesn't define a
rel attribute the value returned by the rel property is a null pointer.
However since it is treated as an empty string even === "" will return true.
There is simply no way to tell. If the attribute rel is present on the
element as rel="" or is assigned "" directly in code its value is actually
an empty string.


It would seem that getElementById objects to a null pointer whereas its
happy to accept an empty string. This is an over simplification since I
have been able to pass a null pointer to getElementById in VB when passing
in a string variable directly, however it does fail when a string expression
results in a null pointer.


--
Anthony Jones - MVP ASP/ASP.NET


0 new messages