Thanks, Kangax. I think that helps answer my question. I was hoping my
cookiejar class would be able to store *any* JS object in the client cookie,
but now I realize that it's not easy and probably unneccessary.

-Hector


On Fri, Nov 7, 2008 at 11:50 AM, kangax <[EMAIL PROTECTED]> wrote:

>
> On Nov 7, 2:19 pm, "Hector Virgen" <[EMAIL PROTECTED]> wrote:
> > Hello,
> >
> > Is it possible to convert elements / text nodes to JSON using
> > Object.toJSON()? I need them in JSON so I can store them in a cookie for
> the
> > cookiejar class I am creating.
> >
> > sample code:
> >
> > var element = new Element('div');
> > var nativeelement = document.createElement('div');
> > var textnode = document.createTextNode('this is text');
> >
> > alert('element: ' + Object.toJSON(element)); // <-- undefined
> > alert('nativeelement: ' + Object.toJSON(nativeelement)); // <-- undefined
> > alert('textnode: ' + Object.toJSON(textnode)); // <-- firefox hangs, "too
> > much recursion" on line 546
>
> `Object.toJSON` delegates its execution to `toJSON` method of an
> object passed to it (if one exists). Therefore, you can give all
> elements such method and enjoy element support in `Object.toJSON` "for
> free" : )
>
> A very naive (and untested) method would be along these lines:
>
> Element.addMethods({
>  toJSON: function(element){
>    if ((element = $(element)) && element.firstChild) {
>      return element.firstChild.nodeValue;
>    }
>    return null;
>  }
> });
>
> Object.toJSON(new Element('div').update('foo')); // 'foo'
> Object.toJSON(new Element('div')); // null
> Object.toJSON(new Element('div').update('<p>foo</p>')); // null
>
> You could obviously go further and extract text from all text nodes
> recursively or use native `innerText`/'textContent'.
>
> >
> > -Hector
>
> --
> kangax
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to