I like optimized code as much as the next guy... but brevity and
readability is KEY. the milliseconds that you can save using one
reasonable technique vs. another are not comparable  to the seconds it
takes for an http request.

I never liked using XMLSerializer because I can't hit the tags &
attributes as I serialize. Of course if you just want to copy the dom
into a string, it does the job for you pretty well. And the addition
of .xml seems like it will even keep IE happy. I guess we can expect
the next cool browser to implement XMLSerializer, but a final else
might be nice to handle stupid versions of js. Or at least an
alert("stupid browser does not support XMLSerializer").

Jake

On 10/2/06, Christof Donat <[EMAIL PROTECTED]> wrote:
> Hi,
>
> > $.fn.serializeXML = function () {
> >       var out = '';
> >       if (typeof XMLSerializer == 'function') {
> >               var xs = new XMLSerializer();
> >               this.each(function() {
> >                       out += xs.serializeToString(this);
> >               });
> >       } else if (this[0] && this[0].xml != 'undefined') {
> >               this.each(function() {
> >                       out += this.xml;
> >               });
> >       }
> >       return out;
> > };
>
> Maybe this could make the code simpler:
>
> if( typeof XMLSerializer != 'function' ) {
>         XMLSerializer = function() {};
>         XMLSerializer.prototype.serializeToString = function(elm) {
>                 return eml.xml;
>         }
> }
>
> Then you just need
>
> $.fn.xml = function () {
>         var out = '';
>         var xs = new XMLSerializer();
>         this.each(function() {
>                 out += xs.serializeToString(this);
>         });
>         return out;
> };
>
> Advantages:
> 1. Your code checks for XMLSerializer with every call. Mine only once.
> 2. You can add another check to see if there is a .xml-Property. If that is
> not available you can have your own (slower) serializer-function that simply
> traverses the DOM.
> 3. By resembling the XMLSerializer-API (not complete now) you can make third
> party code work that needs XMLSerializer.
>
> BTW.: With such a simple function in each() you might also consider to use a
> normal for-loop:
> for( var i = 0; i < this.length; i++ ) out += xs.serializeToString(this[i]);
>
> That doesn't look so fancy, but it should be faster.
>
> Christof
>
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/
>


-- 
Ⓙⓐⓚⓔ - יעקב   ʝǡǩȩ   ᎫᎪᏦᎬ
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to