Adding a script to the page dynamically with jQuery never uses the cached file

前端 未结 3 823
傲寒
傲寒 2020-12-15 23:07

I am using jQuery to dynamically add a script to my page and it works, but jQuery appends \"_=TIMESTAMP\" to the URL causing the browser to never use the cache. With the fo

3条回答
  •  甜味超标
    2020-12-16 00:09

    You can use $.ajax to get the script instead of appending script tag

    $.ajax({
      url: "http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js",
      dataType: "script",
      cache: true,//This will decide whether to cache it or no so that it will not add the timestamp along with the request
      success: function(){}//In the success handler you can write your code which uses resources from this js file ensuring the script has loaded successfully before using it
    });
    

提交回复
热议问题