Hello!
I previously posted this ticket: http://dev.jquery.com/ticket/1598, but by solving that problem for v1.2.1, another one has been created:
When HTML code is added to the DOM (by domManip function), the <SCRIPT> elements it contains should be automatically executed. The problem is that this execution now takes place before the HTML code is inserted!
I corrected this behavior within jQuery 1.2.1, so that the JavaScript code is executed after the DOM manipulation, here is my code from line 385 of the full version:
domManip: function(args, table, dir, fn) {
var clone = this.length > 1, a;
return this.each(function(){
if ( !a ) {
a = jQuery.clean(args, this.ownerDocument);
if ( dir < 0 )
a.reverse();
}
var obj = this;
if ( table && jQuery.nodeName(this, "table") && jQuery.nodeName(a[0], "tr") )
obj = this.getElementsByTagName("tbody")[0] || this.appendChild(document.createElement("tbody"));
jQuery.each( a, function(){
var elem = clone ? this.cloneNode(true) : this;
// Changes start from here ========================================
var script = extractScript(-1, elem);
fn.call( obj, elem );
if (script !== false)
evalScript(script);
});
});
}
};
function extractScript(i, elem) {
if ( jQuery.nodeName(elem, "script") ) {
if ( elem.parentNode )
elem.parentNode.removeChild(elem);
// Don't need to return a jQuery object if it was a recursive call
if (i == -1)
return jQuery(elem);
else
return;
} else if ( elem.nodeType == 1 )
return jQuery("script", elem).each(extractScript);
return false;
}
function evalScript(script) {
script.each(function() {
if ( this.src )
jQuery.ajax({ url: this.src, async: false, dataType: "script" });
else
jQuery.globalEval( this.text || this.textContent || this.innerHTML || "" );
});
}
What I did, was to split the current evalScript function into two function: one that extracts (and removes) the scripts and one that evaluates what has been extracted. This way, the DOM manipulation can be done after the scripts have been extracted (else Firefox will execute the script twice), but before the scripts are executed.
I didn't extensively test my correction. It solves the differences with JavaScript execution for the different browsers in my case. But I tried to make it in the jQuery logic, so that it can be added easily to the next build after having been reviewed by you. ;-)
Thanks for your work!
Gabriel