我在我的应用中使用标记包含了一个外部.js文件。 是否可以使用缓存版本,直到更新版本的.js可用?
如果有更新版本,则应用应使用更新的版本。
答案 0 :(得分:1)
使用以下方法使其工作:
将 fileTransfer.download 与标题
一起使用headers: {
"If-Modified-Since": <last download date>
}
例如
var targetPath = cordova.file.dataDirectory + "external.js"
fileTransfer.download(
uri,
targetPath,
function(entry) {
var success = function(status) {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("id", 'addedScript1');
script.setAttribute("src", targetPath ;
document.getElementsByTagName("head")[0].appendChild(script);
}
var error = function(status) {
alert('Error: ' + status);
}
window.localStorage.setItem("last_modified", new Date(new Date()).toGMTString());
window.cache.clear( success, error );
},
function(error) {
console.log(JSON.stringify(error));
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("id", 'addedScript');
script.setAttribute("src", targetPath
document.getElementsByTagName("head")[0].appendChild(script);
},
false,
{
headers: {
"If-Modified-Since": window.localStorage.getItem("last_modified")
}
}
);