Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Different Type of String Value?

View parsed - Show only message text

From: "Anthony Jones" <A...@yadayadayada.com>
References: <ec1e3830-0a1e-45bd-ba54-bcca1af2a393@x35g2000hsb.googlegroups.com>
Subject: Re: Different Type of String Value?
Date: Tue, 3 Jun 2008 21:55:18 +0100
Lines: 115
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1914
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1914
Message-ID: <ekN1iwbxIHA.4772@TK2MSFTNGP03.phx.gbl>
Newsgroups: microsoft.public.scripting.jscript
NNTP-Posting-Host: 82-37-88-57.cable.ubr04.dudl.blueyonder.co.uk 82.37.88.57
Path: g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!news.astraweb.com!border2.newsrouter.astraweb.com!newshub.sdsu.edu!news.alt.net!msrtrans!TK2MSFTFEEDS02.phx.gbl!TK2MSFTNGP01.phx.gbl!TK2MSFTNGP03.phx.gbl


"dhtml" <dhtmlkitc...@gmail.com> wrote in message
news:ec1e3830-0a1e-45bd-ba54-bcca1af2a393@x35g2000hsb.googlegroups.com...
> 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?
>

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



Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google