看起来jQuerys的evalscript函数让您感到困惑…
jQuery的543行:
function evalscript( i, elem ) { if ( elem.src ) jQuery.ajax({ url: elem.src, async: false, dataType: "script" }); else jQuery.globaleval( elem.text || elem.textContent || elem.innerHTML || "" ); if ( elem.parentNode ) elem.parentNode.removeChild( elem );}这是发生的情况的细分:主页的JS调用:
$.ajax({ url:"frag.htm", type:"GET", success:callBackFunction})和GET frag.htm,其中包含以下内容:
<html><head><script src="test.js"></script></head><body>Content</body></html>
然后您的回调函数将被调用,看起来像这样:
function callBackFunction(data){ $("#ajaxContent").html(data); // <- this is the beginning of your problems...}当调用jQuery的html(data)函数时,它会通过删除任何脚本标签来“清理”
HTML,然后在每个脚本标签上调用evalscript。如您所见,evalscript没有指定“ cache:true”,因此当它通过$
.ajax时,缓存为空。当cache为null且dataType为“ script”时,jQuery设置cache = false。
因此,为避免此问题,请尝试以下操作:
function callBackFunction(data){ var tempAJAX = $.ajax; // save the original $.ajax $.ajax=function(s){ // wrap the old $.ajax so set cache to true... s.cache=true; tempAJAX(s); // call old $.ajax } $("#ajaxContent").html(data); // insert the HTML and download the <script>s $.ajax = tempAJAX; // reset $.ajax to the original. }}在将新的HTML文件从“ frag.htm”插入主页之前,我们将拦截对$ .ajax的所有调用,将对象修改为包括cache =
true,然后在加载脚本后插入HTML。
如果您有任何疑问,请告诉我。



