Let HTML pages not cache js implementation methods

  • 2020-03-30 04:12:22
  • OfStack

This article illustrates how to implement HTML pages without caching js. Share with you for your reference. The specific implementation method is as follows:

Many of you will encounter this situation: if our page is loaded with js, the next time we open it, we will also call this js cache file, but it is very inconvenient for us to debug, this article will talk about how to solve this problem, let's take a look.

The method of not caching JS is actually quite simple, and CSS can be used in certain conditions;

Let's take a look at a simple principle of not caching:

When browsing different urls, the browser will automatically cache the currently accessed address once. The second visit is to call the cached page, so as to achieve the goal of quick page loading (page loading optimization);

Therefore, we can set a different value at the end of the page, so that the page is not cached!

Here's a simple example:

<script> 
document.write("<script type='text/javascript' src='/js/test.js?"+Math.random();+"'></script>");
</script>

For others, just add + math.random () after the address

Note: because math.random () only works in Javascript, it can only work through Javascript calls

Finally add an ajax method that also does not allow caching

xmlHttp.open("GET", "ajax.asp?now=" + new Date().getTime(), true);

Remember that now=" + new Date(). GetTime () is the key.


Related articles: