避免 jQuery Mobile 使用 _=TIMESTAMP 查询字符串参数强制重新加载脚本/CSS

发布于 2024-10-20 13:39:54 字数 547 浏览 3 评论 0原文

据我所知,如果您想将 JavaScript 或 CSS 文件与通过 ajax 自动加载的特定页面一起加载,那么您必须将 CSS/JavaScript 引用放在

示例:

<div data-role="page" data-theme="e">
  <script type="text/javascript" src="/js/jquery/plugins/plugins.js"></script>

一般来说,这工作得很好。然而,在此过程中,脚本 URL 被修改:

/js/some_sepcial_script.js becomes e.g. js/some_sepcial_script.js?_=1299308309681

其中 1299308309681 是当前的 Unix 时间戳,它会根据每个请求而更改,从而阻止缓存。我很确定这是预期的行为,但是如果您想让文件可缓存,有谁知道如何防止将时间戳附加到脚本/CSS url 中?

As far as I know, if you want to load JavaScript or CSS files together with a specific page that is automatically loaded via ajax then you have to put the CSS/JavaScript references within the <div data-role="page"> container.

Example:

<div data-role="page" data-theme="e">
  <script type="text/javascript" src="/js/jquery/plugins/plugins.js"></script>

In general, this works fine. However, somewhere along the way, the script url gets modified:

/js/some_sepcial_script.js becomes e.g. js/some_sepcial_script.js?_=1299308309681

Where 1299308309681 is the current Unix timestamp which changes on every request and thus prevents caching. I am pretty sure that this is intended behaviour but does anyone know how you can prevent the timestamp from being appended to the script/CSS urls if you want to make the file cacheable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-黛色若梦 2024-10-27 13:39:54

你尝试过吗:?

$.ajax ({
    // Disable caching of AJAX response */
    cache: false
});

它应该全局更改 ajax 请求。我只是不确定外部脚本。

[编辑]

这是 jquery mobile 1.0a3 涉及的源代码:

var all = $("<div></div>");
                //workaround to allow scripts to execute when included in page divs
                all.get(0).innerHTML = html;
                to = all.find('[data-role="page"], [data-role="dialog"]').first();

                //rewrite src and href attrs to use a base url
                if( !$.support.dynamicBaseTag ){
                    var newPath = path.get( fileUrl );
                    to.find('[src],link[href]').each(function(){
                        var thisAttr = $(this).is('[href]') ? 'href' : 'src',
                            thisUrl = $(this).attr(thisAttr);

                        //if full path exists and is same, chop it - helps IE out
                        thisUrl.replace( location.protocol + '//' + location.host + location.pathname, '' );

                        if( !/^(\w+:|#|\/)/.test(thisUrl) ){
                            $(this).attr(thisAttr, newPath + thisUrl);
                        }
                    });
                }

那里没有添加缓存阻止参数。

[编辑2]

我知道这超出了解决问题的方法,但是您是否尝试过动态加载js,如下所述: http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

(我知道它可以通过 jQuery 完成,但出于测试目的,我试图避免使用 jQuery)

Have you tried:?

$.ajax ({
    // Disable caching of AJAX response */
    cache: false
});

It should globally change ajax requests. I'm just not sure about external scripts.

[EDIT]

This is the source code involved for jquery mobile 1.0a3:

var all = $("<div></div>");
                //workaround to allow scripts to execute when included in page divs
                all.get(0).innerHTML = html;
                to = all.find('[data-role="page"], [data-role="dialog"]').first();

                //rewrite src and href attrs to use a base url
                if( !$.support.dynamicBaseTag ){
                    var newPath = path.get( fileUrl );
                    to.find('[src],link[href]').each(function(){
                        var thisAttr = $(this).is('[href]') ? 'href' : 'src',
                            thisUrl = $(this).attr(thisAttr);

                        //if full path exists and is same, chop it - helps IE out
                        thisUrl.replace( location.protocol + '//' + location.host + location.pathname, '' );

                        if( !/^(\w+:|#|\/)/.test(thisUrl) ){
                            $(this).attr(thisAttr, newPath + thisUrl);
                        }
                    });
                }

Nothing on there adds a cache preventing param.

[EDIT 2]

I know this goes beyond troubleshooting to a work around but have you tried dynamically loading the js like explained here: http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml

(I know it can be done through jQuery but for testing purposes I'm trying to avoid jQuery)

旧情别恋 2024-10-27 13:39:54

如果我包含 jQuery 1.4.3 而不是 1.5,一切都会正常工作。这对我来说是一个足够的解决方案。再次感谢您的支持。

if I include jQuery 1.4.3 instead of 1.5 everything works fine. That's a sufficient solution for me. Thanks again for your support.

落叶缤纷 2024-10-27 13:39:54

尝试跑步:

$.ajaxPrefilter("script", function (s) {
    if (s.cache === undefined) {
        s.cache = true;
    }
});

它会改变这种行为吗?

Try running:

$.ajaxPrefilter("script", function (s) {
    if (s.cache === undefined) {
        s.cache = true;
    }
});

Does it change this behavior?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文