Wednesday, April 04, 2007

Fix for jQuery bug in IE - Working with XML documents

April 10th 2007 - UPDATE:
Official patch released -
see details. Use the information below for reference only.

It's been driving me crazy and I'm not alone (1, 2). But I've finally managed to fix the weird jQuery bug in IE when working with XML documents (or so I hope).

The bug isn't in jQuery itself, it's in the very popular Metadata plug-in, used to load meta data and settings from elements using the class property.

The Error
Lines 101 - 105 in the Metadata plugin.

if ( $.meta.single )
this[ $.meta.single ] = data; // Throws error on XML documents in IE
else
$.extend( this, data ); // Throws error on XML documents in IE

this.metaDone = true; // Throws error on XML documents in IE

The fix
Find lines 75-77 in the Metadata plugin:
$.fn.setArray = function(){
return setArray.apply( this, arguments ).each(function(){
if ( this.metaDone ) return;
And make the following change: (add line)
$.fn.setArray = function(){
return setArray.apply( this, arguments ).each(function(){
try{ this['meta']=null; }catch(e){ return; } // Detect and trap error
if ( this.metaDone ) return;
And that's it! This fix will allow Metadata to do what it does on HTML elements and not throw that obscure "Object doesn't support this property or method" error in Internet Explorer.

13 comments:

mattyd said...

Good stuff Diego! I will have a play...

Diego A. said...

Let me do know how you get on, post back here if you still have problems...

Cheers,
Diego A.

Mark said...
This post has been removed by a blog administrator.
Mark said...

Diego, you are an absolute star! I've been struggling with this for ages and I really hate using the long winded DOM methods.

This worked like a dream! And I can't believe you only posted it two days ago!

Have you told the author of the metadata plugin? Maybe he could be persuaded to update his code with your fix?

Mark.

Diego A. said...

I haven't contacted the author(s) yet because I wanted to make sure it was really the plugin causing the problem. The plugin is pretty great and I didn't want to offend the author(s) ;-)

But now I know it also worked for you, I've posted a comment on the project page and hopefully the fix will be included in future updates.

Metadata Plugin Project Page

Cheers,
Diego A.

Jörn said...

Hey Guys,

the actual author of the metadata plugin is John Resig, but I've maintained it. I've commited a similar fix just a few days ago, let me know if that works for you!

Diego A. said...

Hi Jorn,

I wasn't sure who to contact because the plugin source doesn't give a URL or contact email...

This fix has worked so it would be great if you could pass it on to the authors.

What was your fix?

Thanks,
Diego A.

Diego A. said...
This post has been removed by the author.
kenny said...

I'm also struggling with this IE "feature", could you provide a compressed jquery.js with this fix so I can see if it works for me as well?

Diego A. said...

Hi Kenny,

The error isn't in the jquery library - it's in the metadata plugin.

I setup a page here with details about the bug and a copy of both metadata plugins, before and after the fix.

Diego A. said...

UPDATE:
Official patch released.
Use this post for reference only.

Alan said...

We are come here to solve all types of SEO problems.Just visit our following website:
Wavestech - SEO Services,

Link building service,Search Engine Optmization,

Submission Services,Internet marketing services,

PPC,web designing and all Website Promotion services

Parul said...

Web Art Sense is the leading web design company creating online history by its presence. Web art Sense is a platform for our clients to have very impactful and meaningful designs with the best user experience.

Web Art Sense a web design company is here for a purpose to make art and design of your imagination alive and real on the web platform. After several months of hunting of the best talent and struggling for the name of their design company they finally reached a vertical which engages their vision of togetherness. The best talented people in web designs have joined hands to compete and provide innovative, unique concepts to users/clients all across the globe. Web Art Sense is not any design company; they take real care of having your brands be presented that invokes your revelation to others who see it.
Web Design Company