Hello I am trying make a browser cache experiment. like this
<head>
<script type="text/javascript" src='<%=Tool.GetFileRefUrl("js_cached.js")%>' ></script>
<script type="text/javascript">
var lastFile = fun();
</script>
<script src="js_cached.js" type="text/javascript"></script>
<script type="text/javascript">
var cacheFile = fun();
</script>
</head><body>
<form>
<div>
The cache file is :<script type="text/javascript">
document.writeln(cacheFile);
</script>
<br />
The last file is :<script type="text/javascript">
document.writeln(lastFile);
</script>
</div>
</form>
</body>
Tool.GetFileRefUrl("js_cached.js")
will return a random js file string every time like "js_cached.js?v=8752162122311"
The first time the fun()
runs it returns "A", so the page will show:
The cache file is :A
The last file is :A
Now I changed the file js_cached.js
which on the web server and make fun()
return "B" and I hope too see the result:
The cache file is :A
The last file is :B
but it actually is:
The cache file is :B
The last file is :B
Is something wrong? Why does the browser not cache the js file? Did I make an incorrect assumption?
the fun is
function fun() {
return "A";//after modify it be "B"
}
the Tool.GetFileRefUrl is
public class Tool
{
public static string GetFileRefUrl(string url)
{
var ticks = GetLastWriteTime(url);
var result = string.Format("{0}?v={1}", url, ticks);
return result;
}
private static long GetLastWriteTime(string fileName)
{
var lastWriteTime = File.GetLastWriteTime(HttpContext.Current.Server.MapPath(fileName));
return lastWriteTime.Ticks;
}
}