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

String from Javascript DOM object

0 views
Skip to first unread message

Tim

unread,
Jul 4, 2009, 1:35:49 PM7/4/09
to
Need help! I am a little stuck. I am trying to get the string
representation of a object that I created in memory.

var fieldImage = document.createElement("img");
fieldImage.src = domObj.fieldImage;

I am trying to get the formatted string for: fieldImage like "<img
src="...."/>" but don't see a method to do that.

Ideas? Help!

Martin Honnen

unread,
Jul 4, 2009, 1:39:24 PM7/4/09
to

fieldImage.outerHTML
is what IE 4 introduced and by now most browsers beside Mozilla browsers
offer.

--

Martin Honnen
http://msmvps.com/blogs/martin_honnen/

Thomas 'PointedEars' Lahn

unread,
Jul 4, 2009, 2:32:46 PM7/4/09
to
Martin Honnen wrote:
> Tim wrote:
>> Need help! I am a little stuck. I am trying to get the string
>> representation of a object that I created in memory.
>>
>> var fieldImage = document.createElement("img");
>> fieldImage.src = domObj.fieldImage;
>>
>> I am trying to get the formatted string for: fieldImage like "<img
>> src="...."/>" but don't see a method to do that.
>
> fieldImage.outerHTML
> is what IE 4 introduced and by now most browsers beside Mozilla browsers
> offer.

outerHTML will not yield the true source code, though. It will also not
work as expected with XHTML, which is what the OP might want (if the `/' in
the start tag is any indication). Fortunately, XML serializers are built-in
at least in Gecko:

var s = new XMLSerializer().serializeToString(
nodeRef, "application/xhtml+xml");

It is rather unclear why the OP would want the source code when he has the
object; however, if the built-in serializers are not satisfactory, it is
rather easy to create one (merely an exercise in string concatenation) as
the properties that need to be accessed are known (especially here as the OP
appears to assign them himself) and can be feature-tested. Only
transforming event-handler attributes back to string might prove a bit tricky.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

Jorge

unread,
Jul 4, 2009, 3:51:57 PM7/4/09
to

alert(fieldImage.outerHTML);

--
Jorge.

0 new messages