如何停止的jQuery附加一个唯一的ID,通过AJAX调用的脚本? [英] How do I stop jquery appending a unique id to scripts called via ajax?

查看:156
本文介绍了如何停止的jQuery附加一个唯一的ID,通过AJAX调用的脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery的Ajax功能抢到一个页面片段,并显示在页面的一部分 - 这片段中包含了HTML和引用外部js文件

I'm using jquery's ajax functions to grab a page fragment and display in a section of a page - this fragment includes html and references to external js files.

程序流程是这样的:

首页电话 - >片段网页,其中调用 - >各种大型js文件通过脚本标签

Main Page calls -> Fragment page which calls -> various large js files via script tags.

我已经打开了缓存选项在我最初的ajax调用,使得fragement页面被缓存(追加到URL中有唯一的ID),然而,当片段被加载,似乎jQuery的重写剧本的网址包括Unix时间戳,使得浏览器下载脚本,每次一个新的副本。我打电话的脚本是围绕250KB缩小的,这真的伤害了用户体验为浏览器锁死时,他们是所谓的。这是jQuery的理想行为?有没有一种方法来禁用URL重写?

I've turned on the cache option on my initial ajax call so that the fragement page gets cached (no unique IDs appended to the url), however when the fragment is loaded, it appears that jquery rewrites the script urls to include a unix timestamp so that the browser downloads a new copy of the scripts every time. The scripts i'm calling are around 250kb minified and it's really hurting the user experience as the browser locks up whenever they're called. Is this a desired behaviour of jquery? Is there a way to disable the url rewrites?

许多许多感谢您的帮助

推荐答案

看起来像jQuerys的evalScript功能是搞乱你了...

Looks like jQuerys's evalScript function is messing you up...

jQuery的543线:

Line 543 of jQuery:

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调用:

Here is the breakdown of what happens: The Main Page's JS calls:

$.ajax({
    url:"frag.htm",
    type:"GET",
    success:callBackFunction
})

和GET frag.htm其中包含这样的:

and GETs frag.htm which contains something like this:

<html><head><script src="test.js"></script></head><body>Content</body></html>

那么你的回调函数被调用这很可能是这样的:

then your callback function is called which probably looks like this:

function callBackFunction(data){
    $("#ajaxContent").html(data); // <- this is the beginning of your problems...
}

在jQuery的HTML(数据)函数被调用为清除的HTML通过删除任何脚本标签,然后在每一个来电evalScript。 evalScript,你可以看到,不指定缓存:真,所以当它通过$阿贾克斯缓存为空。当缓存为空和数据类型为脚本的jQuery设置缓存=假的。

When jQuery's html(data) function is called is "cleans" the HTML by removing any script tags and then calls evalScript on each one. evalScript, as you can see, doesn't specify "cache:true" so when it goes through $.ajax cache is null. When cache is null and the dataType is "script" jQuery sets cache=false.

因此​​,要解决这个问题试试这个:

So, to circumvent this problem try this:

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.
    }
}

在我们插入从frag.htm新的HTML进入主页,我们拦截所有调用$阿贾克斯,修改对象包括缓存= TRUE,再经过脚本加载插入HTML。

Before we insert the new HTML from "frag.htm" into the Main Page we intercept all calls to $.ajax, modify the object to include cache=true, and then after the script is loaded insert the HTML.

让我知道,如果你有任何问题。

Let me know if you have any questions.

这篇关于如何停止的jQuery附加一个唯一的ID,通过AJAX调用的脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆