Does buildFragment cacheable behavior prevent execution of scripts?
Using jQuery 1.4.1. I'm responding to an ajax get request with some html that includes a script tag. I found that the contents of the script tag is not evaluated after the first time it is requested. I've dug into this a bit and was hoping that someone could confirm the issue:
(1) It seems that the fragments built by "buildFragment" are ALWAYS cached (well, under the conditions on line no. 4202). Changing the 'cache' option to false on the $.ajax request does not affect this. Is this correct, or am I missing something here?
(2) The <script> in a cached fragment is not re-evaluated (since line 4215, which populates the "scripts" parameter, is only run when there is no cached fragment). Is this the desired behaviour?
- 4198 function buildFragment( args, nodes, scripts ) {
4199 var fragment, cacheable, cacheresults, doc;
4200
4201 // webkit does not clone 'checked' attribute of radio inputs on cloneNode, so don't cache if string has a checked
4202 if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {
4203 cacheable = true;
4204 cacheresults = jQuery.fragments[ args[0] ];
4205 if ( cacheresults ) {
4206 if ( cacheresults !== 1 ) {
4207 fragment = cacheresults;
4208 }
4209 }
4210 }
4211
4212 if ( !fragment ) {
4213 doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
4214 fragment = doc.createDocumentFragment();
4215 jQuery.clean( args, doc, fragment, scripts );
4216 }
4217
4218 if ( cacheable ) {
4219 jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
4220 }
4221
4222 return { fragment: fragment, cacheable: cacheable };
4223 }
Note, I am able to address my particular issue by adding an additional condition to prevent caching when script tags are present (line 4202) as follows:
- 4202 if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 && !/<script/.test( args[0] ) && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {