Jquery getScript caching

前端 未结 6 937
花落未央
花落未央 2020-12-02 20:48

By Default $.getScript() disables caching and you can use $.ajaxSetup and set caching to true. When testing if the script is actually cached with Firebug most of the time t

6条回答
  •  长情又很酷
    2020-12-02 21:03

    There is an error as of the date this question was posted where both Firefox and Chrome would state that a script is not being loaded from Cache when it indeed is. As of the date of this answer this issue still exists. The easiest way to test is to use console.log and send out a version number.

    To cache a dynamically loaded script it it simply done by using the following code.

    function onDemandScript ( url, callback ) {
        callback = (typeof callback != 'undefined') ? callback : {};
    
        $.ajax({
             type: "GET",
             url: url,
             success: callback,
             dataType: "script",
             cache: true
         });    
    }
    

    For development you should comment out cache: true.

提交回复
热议问题