How to force the browser to receive new JavaScript (JS) files? using script tag in html

I use the Angularjs Framework and I update my js controllers a lot and I want to know how I can get broweser to get new / updated js files and not pull them out of the cache.

If someone can help me, it will be a huge thank you!

+4
source share
1 answer

The easiest way to get an uncached file is to add a line at the end of the file. This could be a version number, a timestamp, or some random string.

PHP:

<?php
echo '<script src="/js/file.js?t=' . time() . '"></script>';

JavaScript:

<script>
    document.head.appendChild(
        document.createElement('script')
    ).src='/js/file.js?t=' + (new Date().getTime());
<script>

For an unused file, change the line for each request each time.

- md5.

+2

Source: https://habr.com/ru/post/1677618/


All Articles