devxlogo

Avoid Unwanted Browser Caching Using JavaScript

Avoid Unwanted Browser Caching Using JavaScript

Sometimes, you’ll notice that when you open a URL (with dynamic content, and especially a pop-up window) a second time, the page displays the data which was displayed earlier and not the data newly expected. One example of this is when you open a pop-up window that shows exported data in Excel. This happens because browser has cached the earlier URL response and is displaying it (even if the content has changed in next request-response cycle). This tip addresses this problem.

What is “browser cache”? The browser has a folder in which certain items that have been downloaded are stored for future access. Graphic images (such as buttons, banners, icons, advertising, graphs, and color bars), photographs, and even entire web pages are examples of cache items. While accessing a web site, your computer will check its cache folder first to see if it already has the contents needed to display the page and, if so, it won’t take the time to download them again. This makes for faster page loading.

Sometimes, the browser caches complete pages that have been visited and displays them in response to the same URL when visited again. The browser feels that it is helping us (and caching does help, but not in this situation).

The simplest way to solve this is to change the URL every time you visit. This can be done by adding a dummy request parameter (like the time) to the URL whose value changes every time you visit the page.

Suppose you have the URL:

window.open (http:\localhost:8080/Web/calcuateInterest.jsp,,)

Change it to this URL :

window.open (http:\localhost:8080/Web/calcuateInterest.jsp?dummyParameter=new Date().getTime(),,);

This URL actually appends the time in milliseconds as the request parameter. This changes the URL on each hit unless you manage to hit same URL in same millisecond.

See also  Why ChatGPT Is So Important Today

Note: The JavaScript method getTime() of the Date object gives time elapsed in milliseconds.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist