Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 9 years ago

#1419 closed bug (fixed)

Can't modify XML/Iframe documents in IE

Reported by: john Owned by: john
Priority: major Milestone: 1.2.2
Component: core Version: 1.1.3
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by john)

Two separate reports of this occurring:

$(document).ready(function(){
       var fred="<hi></hi>";
       console.log(fred);
       // Push through XML processor
       xml = loadXML(fred);

       var b = $("hi:first", xml);
       var node = $("<div class='group' >xxx</div>");
       node.appendTo(b);

});

I'm having a similar problem in IE. However, my code looks like this (using your syntax for creating the XML document):

var hiNode = $("hi", xml);
hiNode.text("foo");

When I run this in Firefox, the hi node in the XML document gets a text node with a value of "foo" appended to it. However, I get a type mismatch error in IE.

I can use the .text() function to retrieve text nodes from XML documents - can I also use it to set text nodes?

Change History (6)

comment:1 Changed 13 years ago by john

Description: modified (diff)

comment:2 Changed 13 years ago by john

Summary: Can't modify XML documents in IECan't modify XML/Iframe documents in IE

Some relevant bugs: #968: $('<tag>') needs optional document parameter to ease cross-frame DOM wrangling #1264: Cross-frame DOM manipulation

comment:3 Changed 13 years ago by cdivilly

I worked around this problem by patching jquery-1.2.1.js as follows:

--- jquery-1.2.1.js	
+++ jquery-1.2.1.js	
@@ -151,8 +151,10 @@
 	},
 
 	text: function(e) {
-		if ( typeof e != "object" && e != null )
-			return this.empty().append( document.createTextNode( e ) );
+		if ( typeof e != "object" && e != null ) {
+		    d = this[0].ownerDocument || document;
+			return this.empty().append( d.createTextNode( e ) );
+		}
 
 		var t = "";
 		jQuery.each( e || this, function(){

The existing code uses the page's DOM to create the text node and then attempts to append the text node to the XML document's DOM. IE does not allow adding a node from one DOM into another. The above patch checks if the current element has an ownerDocument and if so uses that to create the text node instead. Other places where a similar fix might be required, lines: 398 and 2261

comment:4 Changed 13 years ago by davidserduke

Milestone: 1.1.41.2.2
Resolution: fixed
Status: newclosed

Fixed in [4031] and [4029]. Thanks cdivilly for the patch which I only optimized slightly.

comment:5 Changed 10 years ago by anonymous

The following code sample may be helpful to others who reach this page who are having the problem with the fact that IE does not allow adding a node from one DOM into another.

create a pop up window PopUp = window.open(, 'MyPopUp', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=800,height=600,left = 340,top = 112');

create a div element within the document of the pop up window var item_div = PopUp.document.createElement('div'); make a copy of it as a Jquery object on the document of the main page (Note: the pop up does not have the Jquery js file referenced). Use jquery to manipulate the item. var jq_item_div = $(item_div).addClass('item_div').html("Hello World"); return the jquery element into the pop up element item_div = jq_item_div; append the item to the pop up document body $(PopUp.document.body).append(item_div);

comment:6 Changed 10 years ago by anonymous

The following code sample may be helpful to others who reach this page who are having the problem with the fact that IE does not allow adding a node from one DOM into another.

//create a pop up window 
PopUp = window.open(, 'MyPopUp', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1,width=800,height=600,left = 340,top = 112'); 

//create a div element within the document of the pop up window 
var item_div = PopUp.document.createElement('div');
 
//make a copy of it as a Jquery object on the document of the main page 
//(Note: the pop up does not have the Jquery js file referenced). 
//Use JQuery to manipulate the item. 
var jq_item_div = $(item_div).addClass('item_div').html("Hello World");

//return the jquery element into the pop up element 
item_div = jq_item_div; 

//append the item to the pop up document body 
$(PopUp.document.body).append(item_div);

Note: See TracTickets for help on using tickets.