了解jQuery $ .getScript() [英] understanding jQuery $.getScript()

查看:103
本文介绍了了解jQuery $ .getScript()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用getScript动态加载我的插件:

I use getScript to load dynamically my plugin:

$.getScript('js/code.photoswipe.jquery-3.0.4.min.js', function () {
   //do magic
});

  1. 如何禁用缓存清除? 目前,它会在末尾生成数字:js/code.photoswipe.jquery-3.0.4.min.js?_ = 1326992601415 我看到了,但不确定如何在我的情况下使用它:

  1. How do I disable caching busting? At the moment it generates numbers at the end: js/code.photoswipe.jquery-3.0.4.min.js?_=1326992601415 I saw this, but not sure how to use it in my case:

$.getScript = function (url, callback, cache) {
   $.ajax({
      type: "GET",
      url: url,
      success: callback,
      dataType: "script",
      cache: cache
   });
};

  • 如果我多次调用$ .getScript来添加相同的js文件,它是否每次都要求获取该文件?如果是这样,是否有办法检查我们是否已经导入了该脚本,所以我们可以避免对同一文件再次调用getScript?

  • If I call $.getScript multiple times adding the same js file, does it do request each time to get that file? If so, is there a way to check if we already imported that script, so we could avoid calling getScript again for the same file?

    推荐答案

    您的浏览器将已经相应地缓存网址.因此您不必担心缓存.

    Your browser will cache the url accordingly already.. So you don't have to worry about caching.

    但是,如果您想破坏缓存,只需向网址中添加一个随机字符串,如下所示:

    If however you want to bust the cache simply add a random string to the url like so:

    $.getScript('js/code.photoswipe.jquery-3.0.4.min.js?' + Math.random(), function () {
            //do magic
    });
    

    ?' + Math.random()将允许在您的js文件中附加一个随机数,因此每次对该文件的请求都会中断缓存(因为它会随机生成一个数字)

    ?' + Math.random() will allow for a random number to append to your js file so caching is broken with each request for the file (as it randomly generates a number)

    这篇关于了解jQuery $ .getScript()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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