Home > Web Front-end > JS Tutorial > How to prevent js from being cached on html pages_javascript skills

How to prevent js from being cached on html pages_javascript skills

WBOY
Release: 2016-05-16 16:32:42
Original
1610 people have browsed it

The example in this article describes how to prevent js from being cached in html pages. Share it with everyone for your reference. The specific implementation method is as follows:

Many friends will encounter this situation: if our page loads js, the js cache file will be called the next time it is opened, but it is very inconvenient for us when debugging. This article will talk about how to solve it. Let’s take a look at this issue together below.

The method of not caching JS is actually quite simple. CSS can also be used in this way under certain conditions;

Let everyone understand a simple principle of not caching:

When browsing different URLs, the browser will automatically cache the currently visited address once; and on the second visit, the cached page will be called, thereby achieving the purpose of fast page loading (page loading optimization) ;

Therefore, we can set a different value for the back of the page, so that the page can be accessed differently to achieve the purpose of not caching!

Here is a simple example:

Copy code The code is as follows:
<script> <br> document.write("<script type='text/javascript' src='/js/test.js?" Math.random(); "'></script>");

Others are similar, just add Math.random() after the address

Note: Because Math.random() can only work under Javascript, it can only be called through Javascript

Finally add an ajax method to prevent caching

Copy code The code is as follows:
xmlHttp.open("GET", "ajax.asp?now= " new Date().getTime(), true);

Remember now=" new Date().getTime() is the key point and needs to take parameters.

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template