Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to cache the javascript file
    primarykey
    data
    text
    <p>Hello I am trying make a browser cache experiment. like this </p> <pre><code>&lt;head&gt; &lt;script type="text/javascript" src='&lt;%=Tool.GetFileRefUrl("js_cached.js")%&gt;' &gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var lastFile = fun(); &lt;/script&gt; &lt;script src="js_cached.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; var cacheFile = fun(); &lt;/script&gt; &lt;/head&gt;&lt;body&gt; &lt;form&gt; &lt;div&gt; The cache file is :&lt;script type="text/javascript"&gt; document.writeln(cacheFile); &lt;/script&gt; &lt;br /&gt; The last file is :&lt;script type="text/javascript"&gt; document.writeln(lastFile); &lt;/script&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; </code></pre> <p><code>Tool.GetFileRefUrl("js_cached.js")</code> will return a random js file string every time like <code>"js_cached.js?v=8752162122311"</code></p> <p>The first time the <code>fun()</code> runs it returns "A", so the page will show:</p> <blockquote> <p>The cache file is :A<br> The last file is :A</p> </blockquote> <p>Now I changed the file <code>js_cached.js</code> which on the web server and make <code>fun()</code> return "B" and I hope too see the result:</p> <blockquote> <p>The cache file is :A<br> The last file is :B</p> </blockquote> <p>but it actually is:</p> <blockquote> <p>The cache file is :B<br> The last file is :B</p> </blockquote> <p>Is something wrong? Why does the browser not cache the js file? Did I make an incorrect assumption?</p> <p>the fun is</p> <pre><code> function fun() { return "A";//after modify it be "B" } </code></pre> <p>the Tool.GetFileRefUrl is</p> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload