]> source.dussan.org Git - jquery.git/commitdiff
Added a fix for bug #1698, in which injected scripts were executed out of order,...
authorJohn Resig <jeresig@gmail.com>
Wed, 17 Oct 2007 22:42:12 +0000 (22:42 +0000)
committerJohn Resig <jeresig@gmail.com>
Wed, 17 Oct 2007 22:42:12 +0000 (22:42 +0000)
src/core.js
test/unit/core.js

index b3b0a9d27ad64d0fe85992d05366607b081d3fcf..d396ee95a6844161fe6ad507437c7a8afdf6a0fd 100644 (file)
@@ -461,39 +461,51 @@ jQuery.fn = jQuery.prototype = {
                        if ( table && jQuery.nodeName( this, "table" ) && jQuery.nodeName( elems[0], "tr" ) )\r
                                obj = this.getElementsByTagName("tbody")[0] || this.appendChild( document.createElement("tbody") );\r
 \r
+                       var scripts = jQuery( [] );\r
+\r
                        jQuery.each(elems, function(){\r
                                var elem = clone ?\r
                                        this.cloneNode( true ) :\r
                                        this;\r
 \r
-                               if ( !evalScript( 0, elem ) )\r
+                               if ( jQuery.nodeName( elem, "script" ) ) {\r
+\r
+                                       // If scripts are waiting to be executed, wait on this script as well\r
+                                       if ( scripts.length )\r
+                                               scripts = scripts.add( elem );\r
+\r
+                                       // If nothing is waiting to be executed, run immediately\r
+                                       else\r
+                                               evalScript( 0, elem );\r
+\r
+                               } else {\r
+                                       // Remove any inner scripts for later evaluation\r
+                                       if ( elem.nodeType == 1 )\r
+                                               scripts = scripts.add( jQuery( "script", elem ).remove() );\r
+\r
+                                       // Inject the elements into the document\r
                                        callback.call( obj, elem );\r
+                               }\r
                        });\r
+\r
+                       scripts.each( evalScript );\r
                });\r
        }\r
 };\r
 \r
 function evalScript( i, elem ) {\r
-       var script = jQuery.nodeName( elem, "script" );\r
-\r
-       if ( script ) {\r
-               if ( elem.src )\r
-                       jQuery.ajax({\r
-                               url: elem.src,\r
-                               async: false,\r
-                               dataType: "script"\r
-                       });\r
-\r
-               else\r
-                       jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );\r
-       \r
-               if ( elem.parentNode )\r
-                       elem.parentNode.removeChild( elem );\r
+       if ( elem.src )\r
+               jQuery.ajax({\r
+                       url: elem.src,\r
+                       async: false,\r
+                       dataType: "script"\r
+               });\r
 \r
-       } else if ( elem.nodeType == 1 )\r
-               jQuery( "script", elem ).each( evalScript );\r
+       else\r
+               jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );\r
 \r
-       return script;\r
+       if ( elem.parentNode )\r
+               elem.parentNode.removeChild( elem );\r
 }\r
 \r
 jQuery.extend = jQuery.fn.extend = function() {\r
index eee1740d6361d99c959eccbe1d3db98169772cb1..7280ab90087bbcb2513751e30c1ba4e910ffb513 100644 (file)
@@ -834,8 +834,10 @@ test("val(String)", function() {
        ok( $("#select1").val() == "3", "Check for modified (via val(String)) value of select element" );
 });
 
+var scriptorder = 0;
+
 test("html(String)", function() {
-       expect(3);
+       expect(9);
        var div = $("div");
        div.html("<b>test</b>");
        var pass = true;
@@ -850,6 +852,8 @@ test("html(String)", function() {
 
        $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
 
+       $("#main").html("<script>ok(scriptorder++ == 0, 'Script is executed in order');ok($('#scriptorder').length == 0,'Execute before html')<\/script><span id='scriptorder'><script>ok(scriptorder++ == 1, 'Script is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script></span><script>ok(scriptorder++ == 2, 'Script is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script>");
+
        setTimeout( start, 100 );
 });